Overview
Readings Brown & Vranesic §5.1–5.3 · week 5–6
A pattern of bits has no meaning by itself. could be 251, or −5, or −4, or −123: it depends entirely on the representation, the agreed rule for reading it. This chapter is about choosing representations for numbers and building the circuits that do arithmetic on them.
The story has a satisfying ending. There are three reasonable ways to write negative numbers, and one of them, 2’s complement, turns out to be special: with it, a single ordinary adder circuit can add and subtract, positive and negative numbers, with no special cases. That’s why every computer uses it. Along the way the chapter builds that adder from scratch (half adder → full adder → ripple-carry adder), and finishes with overflow, what happens when an answer doesn’t fit in the bits available.
Notes
Unsigned numbers
An -bit unsigned number uses ordinary positional notation (see Ch 1), with each bit worth twice the one to its right:
With bits the range is to (8 bits: 0 to 255). “Unsigned” means every pattern is a non-negative number, so the whole range is spent on 0 and up.
Always know the representation before you read a bit pattern. is 251 unsigned, in 2's complement, in 1's complement, and in sign-magnitude. The hardware doesn't know which one you meant. Only the programmer or designer does.
Octal and hexadecimal
Both are shorthand for long binary numbers, and both work by grouping bits, because their bases are powers of 2. , so one octal digit is exactly 3 bits. , so one hex digit is exactly 4 bits. Conversion is just regrouping, with no arithmetic:
- Octal (base 8): group from the LSB in 3s.
- Hex (base 16): group from the LSB in 4s.
- Pad with 0s on the left if the number of bits isn’t a multiple of 3 or 4: . Leading zeros don’t change a number’s value, but zeros added anywhere else would.
| Binary | Octal | Hex | Decimal | Binary | Octal | Hex | Decimal |
|---|---|---|---|---|---|---|---|
| 0000 | 0 | 0 | 0 | 1000 | 10 | 8 | 8 |
| 0001 | 1 | 1 | 1 | 1001 | 11 | 9 | 9 |
| 0010 | 2 | 2 | 2 | 1010 | 12 | A | 10 |
| 0011 | 3 | 3 | 3 | 1011 | 13 | B | 11 |
| 0100 | 4 | 4 | 4 | 1100 | 14 | C | 12 |
| 0101 | 5 | 5 | 5 | 1101 | 15 | D | 13 |
| 0110 | 6 | 6 | 6 | 1110 | 16 | E | 14 |
| 0111 | 7 | 7 | 7 | 1111 | 17 | F | 15 |
Hex is the usual way to show values from FPGAs and memory, because computers group bits in bytes (8 bits) and one byte is exactly two hex digits.
Example 5.1: Hex, octal, and decimal
Convert to hex, octal, and decimal.
Show answerHide answer
- Hex (groups of 4):
- Octal (groups of 3 from the right, pad to ):
- Decimal: . Check in binary: ✓
The half adder
The simplest addition problem is adding two single bits and . The answer can be 0, 1 or 2, and 2 doesn’t fit in one bit, so the result needs two outputs: a sum bit and a carry bit (the “carry the 1” from column addition):
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
Read each output column on its own and you can recognize the gate:
- The sum column is 1 when exactly one input is 1. That’s XOR.
- The carry column is 1 only when both inputs are 1 (). That’s AND.
It’s called a half adder because it’s missing something: a carry-in. It can add the rightmost column of two numbers, where nothing comes in from the right, but not any other column.
Example 5.2: Half adder on single bits
Find and for a half adder with (a) and (b) .
Show answerHide answer
- (a) , . The pair , which is ✓.
- (b) , . The pair , which is ✓.
The two outputs are just the answer written in two bits: is the 2s place and is the 1s place.
The full adder
Every column except the rightmost has to add three bits: , , and the carry coming in from the column to its right. That’s exactly the carry you write above a column when adding by hand (Figure 5.0). A full adder is one column of that process: it reads the three digits stacked in its column and produces the sum digit and the carry for the column on its left.
Figure 5.0: by columns. The highlighted column is one full adder, with inputs and outputs .
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
Both formulas make sense once you think of the output as just counting the 1s among the three inputs and writing the count in binary (0, 1, 2 or 3 → ):
- The sum bit is the low bit of that count, which is 1 when the count is odd. Chained XORs compute exactly that, since each XOR flips the result whenever it meets another 1.
- The carry bit is the high bit, which is 1 when the count is 2 or more. “At least two of three inputs are 1” is the majority function from Ch 2: any pair being 1 is enough, hence the three AND terms ORed together.
Example 5.3: Full adder outputs
Find and for a full adder with , , , and explain why a half adder couldn’t do this.
Show answerHide answer
- Sum: (an even number of 1s).
- Carry: (two of the three inputs are 1).
So , with and . A half adder only has two inputs, so it has nowhere to take the carry-in.
A full adder can be built from two half adders and an OR gate (Figure 5.1). That’s literally adding in two steps: the first half adder adds and , the second adds that partial sum to . A carry out of either step means the column overflowed, and they can never both carry at once, so an OR combines them.
Figure 5.1: A full adder built from two half adders and an OR gate.
The ripple-carry adder
To add two -bit numbers, do what you’d do on paper: one full adder per column, with each column’s carry passed to the next.
- Full adder adds bits and .
- The carry-out of stage is the carry-in of stage .
- For plain unsigned addition, the first carry-in is (nothing comes in from the right of the rightmost column).
- The last carry-out, , is the extra bit of the sum, the one that doesn’t fit in bits.
The problem is speed. Each full adder can’t settle on its answer until it knows its carry-in, which comes from the stage before, which needs its carry-in, and so on. In the worst case (, say) a carry created in the lowest bit changes every stage, one after another. The result isn’t final until the carry has rippled through all stages, so the worst-case delay grows in proportion to . A 64-bit ripple adder is 64 times slower than one column. Faster adders (carry-lookahead, §5.4) work out all the carries in parallel at the cost of more logic, but they’re not covered in this course.
Example 5.4: Ripple-carry addition
Add and with a 4-bit ripple-carry adder (). Give every carry.
Show answerHide answer
| Stage | |||||
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 | 1 |
| 2 | 1 | 1 | 1 | 1 | 1 |
| 3 | 0 | 0 | 1 | 1 | 0 |
and . Check: ✓
Stage 3 can't settle until arrives, which waits on , which waits on . That chain is what causes the ripple delay.
Signed numbers
To represent negatives, give up the MSB as a sign bit: 0 for positive, 1 for negative. Positive numbers look the same in all three systems. The difference is how the remaining bits describe a negative number:
| Representation | How to negate | 8-bit range | Zeros |
|---|---|---|---|
| Sign-magnitude | flip the sign bit only | to | two ( and ) |
| 1’s complement | flip every bit: | to | two ( and ) |
| 2’s complement | flip every bit, then add 1: | to | one |
- In the formulas, is the positive number being negated, and , are the bit patterns for . For example, with and : and .
- In 4 bits, . Then is (sign-magnitude), (1’s complement), or (2’s complement).
Example 5.5: Three signed representations
Write in 8-bit sign-magnitude, 1’s complement, and 2’s complement.
Show answerHide answer
- Start from :
- Sign-magnitude: set the sign bit →
- 1’s complement: flip every bit →
- 2’s complement: add 1 →
What’s wrong with the first two. Sign-magnitude is how people write numbers (a sign, then a size), so it’s the most natural. But adding two sign-magnitude numbers means comparing signs, then either adding or subtracting the sizes, then working out the sign of the answer. That’s a lot of decision-making hardware. Both sign-magnitude and 1’s complement also have two zeros ( and ), which wastes a pattern and means “is this zero?” needs two checks.
Why 2’s complement wins. It has one zero, and the extra pattern (, which would have been ) becomes one extra negative number, . That’s why the range is lopsided. More importantly, it makes arithmetic automatic, as the next section shows. The reason is the “odometer” idea from Ch 1: is stored as , which is exactly what you’d reach by counting backwards steps from zero on an -bit counter that wraps around.
Reading a 2’s complement number: if the MSB is 0, read it normally. If it’s 1, take the 2’s complement to find the magnitude and put a minus sign in front ( → → ). A neater way to see it: the MSB simply has a weight of instead of , and every other bit keeps its usual positive weight. So .
Example 5.6: Reading a 2’s complement byte
What does represent in (a) unsigned and (b) 8-bit 2’s complement?
Show answerHide answer
(a) Unsigned: .
(b) The MSB has weight and the rest keep their positive weights:
Check by negating: invert → , add 1 → , so the number is ✓. Notice that : a negative 2’s complement number is always its unsigned value minus .
Try it: click bits to see the same pattern read four ways, and use the buttons to take complements.
Adding signed numbers
2’s complement addition always works directly. Add the two bit patterns with an ordinary unsigned adder and ignore any carry out of the sign bit, whatever the signs of the operands:
| Sum | Bits | Result |
|---|---|---|
Why ignoring the carry is correct. is stored as . So is really to the adder. The 16 is exactly the carry out of the 4th bit, and throwing it away leaves 3, the right answer. Because every negative number carries a hidden “+16” (), and the carry-out removes exactly one 16, the arithmetic always comes out right. The adder never needs to know which numbers were negative. That’s the whole point: one adder for every case.
1’s complement addition almost works, but needs a fix. in 1’s complement is , so it carries a hidden “+15” rather than “+16”. , and dropping the carry removes 16, leaving 2, which is one short. So whenever there’s a carry out of the sign bit, you add it back into the LSB (the end-around carry) to make up the difference:
That second addition can double the time an addition takes, which is why 1’s complement isn’t used for arithmetic.
Example 5.7: 1’s complement addition
Add in 4-bit 1’s complement.
Show answerHide answer
- Operands: ;
- Add:
- End-around carry: there’s a carry out, so add it to the LSB:
- Check: , so ✓
2’s complement subtraction: subtracting is adding , and is “flip the bits and add 1”:
So subtraction needs no subtractor circuit at all, just an adder and some inverters.
Example 5.8: Subtraction with 2’s complement
Compute using 8-bit 2’s complement.
Show answerHide answer
- Operands: ,
- Negate 9: invert → , add 1 →
- Add:
- Drop the carry out of the 8-bit word: ✓
Example 5.9: A negative result
Compute using 8-bit 2’s complement.
Show answerHide answer
- Operands: ,
- Negate 25: invert → , add 1 →
- Add: (no carry out)
- Read it: the MSB is 1, so it’s negative. Its 2’s complement is , so the result is ✓
I find it helps to picture the number circle. In 4 bits the patterns to go around a circle like a clock face, with (+7) right next to (−8). Adding moves you clockwise and subtracting moves you counterclockwise. Wrapping past is fine; that’s just the ignored carry. But crossing the +7/−8 boundary means the true answer was off the edge of the range, so the result is wrong (overflow, below).
The adder/subtractor unit
This is the logic behind lab 3, and it’s 2’s complement paying off in hardware. One -bit adder does both operations, chosen by a control signal (0 = add, 1 = subtract; the bar over Add means “add when this is 0”):
- Each bit goes through an XOR gate with the control signal. XOR with 0 passes through unchanged, and XOR with 1 inverts it. So the XORs act as controllable inverters: the adder sees when adding and when subtracting.
- The same control signal also feeds the carry-in . When subtracting, supplies the “+1” that turns into .
The carry-in, which is wasted in plain addition (), turns out to be exactly the spare input needed to add the 1. A whole subtractor costs XOR gates.
Example 5.10: Adder/subtractor unit
A 4-bit adder/subtractor has , , and the control set to Sub (1). What does the adder see, and what’s the output?
Show answerHide answer
- XOR gates invert each : the adder’s second input is .
- Carry-in .
- Add:
- Result , and ✓. , , so no overflow.
Try it: set and , switch between add and subtract, and follow the carries through the four full adders.
Arithmetic overflow
With bits, a 2’s complement result has to be between and (4 bits: −8 to +7). If the true answer is outside that range, there’s simply no pattern for it, and the bits that come out mean something else. That’s arithmetic overflow.
- It can only happen when adding two numbers with the same sign. A positive plus a negative always lands between the two numbers, so it can’t be further from zero than either of them, and it always fits.
- Sign check: overflow happened if two positives give a negative, or two negatives give a positive. On the number circle, you went so far that you wrapped past the +7/−8 boundary into the other half.
- Carry check (what the hardware uses): overflow = , the carry out of the sign bit XOR the carry into it.
Why the carry check works. Look at what happens in the sign-bit column:
- Two positives (sign bits both 0): the sign column can never carry out, so . If a carry comes in (), it turns the sign bit to 1 and the answer looks negative. That’s overflow exactly when , i.e. when the two carries differ.
- Two negatives (sign bits both 1): always carries out, so . If no carry comes in (), the sign bit becomes 0 and the answer looks positive. Overflow again happens exactly when the carries differ.
- Mixed signs: the sign column has a 0 and a 1, so it carries out exactly when a carry came in, and the two carries always match. No overflow, as expected.
In every case, overflow ⟺ the carry in and the carry out of the sign bit disagree, which is one XOR gate.
| Sum (4-bit) | Bits | Overflow? | ||
|---|---|---|---|---|
| 0 | 1 | Yes (got −7, not +9) | ||
| 0 | 0 | No (−5) | ||
| 1 | 1 | No (+5) | ||
| 1 | 0 | Yes (got +7, not −9) |
Example 5.11: Detecting overflow
In 4-bit 2’s complement, does (a) or (b) overflow?
Show answerHide answer
(a) :
- Bits 0–1 don’t produce any carries, bit 2 is so , and bit 3 is so .
- The result is and , so it’s overflow. Two positives gave a negative (), and the true answer needs more than 4 bits.
(b) :
- Bits 0–2 don’t produce any carries (), and bit 3 is so .
- The result is and , so it’s overflow. Two negatives gave a positive (), and the true answer is below .
For unsigned numbers the rule is different. All bits are magnitude, so the result is wrong whenever the sum needs an th bit, which means . The same adder produces both flags, and it's up to whoever reads the result to know which one applies.
Key formulas
Unsigned value
- : bit ( is the LSB)
- : number of bits
Half adder
Full adder
- , : the two operand bits at position
- : carry into position
- : sum bit at position
- : carry out of position (into position )
Complements
- : the positive number being negated
- , : its 1’s and 2’s complement (the bit patterns for )
2’s complement subtraction
- : with every bit inverted
Signed range (-bit 2’s complement)
Overflow (2’s complement)
- : carry out of the sign bit
- : carry into the sign bit
Common mistakes
- Reading a bit pattern without settling on the representation first.
- Using a half adder where there’s a carry-in. Only bit 0 can use one.
- Forgetting the +1 when subtracting with 2’s complement (in hardware it’s the carry-in).
- Keeping the carry out of the sign bit in 2’s complement. It gets ignored (in 1’s complement it’s added back).
- Calling it overflow whenever there’s a carry-out. For signed numbers, check (or the signs) and not just .
- Thinking the 8-bit 2’s complement range is symmetric. It’s −128 to +127.