Representing and Adding Binary Numbers
How can a fixed collection of bits represent positive and negative integers?
From Gate Logic to Arithmetic
Before building arithmetic hardware, we must agree on what each bit pattern means.
Positional Notation: Decimal and Binary
Decimal: powers of 10
5 × 103 + 1 × 102 + 0 × 101 + 9 × 100 = 5109
Binary: powers of 2
1 × 24 + 0 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 21
The subscript identifies the base. The position of each digit determines the power used as its weight.
Bit Patterns and Their Interpretation
000101012101000001the code for A11101100...an operation and its operands11001010one encoded intensityThe same bits can mean different things. Their interpretation depends on how the computer uses them.
Word Size, Registers, and Capacity
A register is a small, fixed-width storage location inside a processor. It holds one bit pattern while the processor works with it.
Word size is the number of bits used for such a basic chunk of information. Common word sizes are 8, 16, 32, and 64 bits.
As unsigned integers: 0 through 255.
In general, an n-bit register has 2n patterns and can represent unsigned values from 0 through 2n−1.
Most and Least Significant Bits
The leftmost bit in the written representation.
The rightmost bit; it determines whether an unsigned number is even or odd.
Next: Add binary numbers one column at a time.
One-Bit Addition Rules
0 + 0sum 0, carry 00 + 1sum 1, carry 01 + 0sum 1, carry 01 + 1sum 0, carry 1because (10)2 = 2Begin at the LSB, write the sum bit, and carry any extra 1 into the next column.
Example: Four-Bit Binary Addition
0+1=1.1+1=(10)2: write 0, carry 1.1+0+carry 1=(10)2: write 0, carry 1.0+0+carry 1=1.
(0110)2=6 and (0011)2=3; therefore (1001)2=9.
Fixed-Width Unsigned Overflow
The mathematical result is (10010)2=18.
A 4-bit register has room only for 0010. The leftmost carry lies outside the register.
Hardware always has a fixed width; it cannot keep adding new bit positions forever.
Four-Bit Binary Addition
0101 + 0011
1010 + 0101
1110 + 0111
A: 1000
B: 1111
C: 0101, carry out 1
Next: Use the same bit patterns to represent negative numbers.
Representing Negative Integers
An n-bit register has exactly 2n patterns.
Use exactly half for non-negative values and half for negative values.
Keep addition hardware the same for positive and negative operands.
The representation used by almost all modern computers is called two's complement.
Four-Bit Two's Complement
| Pattern | Value |
|---|---|
| 0000 | 0 |
| 0001 | 1 |
| 0010 | 2 |
| ... | ... |
| 0111 | 7 |
| Pattern | Value |
|---|---|
| 1000 | −8 |
| 1001 | −7 |
| ... | ... |
| 1110 | −2 |
| 1111 | −1 |
The MSB acts as a sign indicator, but two's complement is not merely “attach a sign bit.”
Range of n-Bit Signed Integers
For 4 bits: 8 negative values, 7 positive values, and zero.
Which Integers Fit in Four-Bit Two's Complement?
Write the four-bit representation of each integer. If it cannot be represented, say why.
A: 0110
B: 1111
C: 1011
D: 1000
E: impossible; the signed range ends at +7
Converting Two's Complement to Decimal
First step: inspect the Most Significant Bit (MSB), then use the corresponding conversion below.
01010 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 5
1011As unsigned, 1011 is 11. For four bits, subtract 24:
11 − 16 = −5
General rule: if an n-bit pattern has MSB 1 and unsigned value U, its two's-complement value is U − 2n.
1011→flip bits→0100→add 1→0101 = 5→value = −5Convert Two's Complement to Decimal
Interpret each pattern as a four-bit two's-complement integer. For C, verify the answer using both conversion methods.
0110111110101000A: 6
B: −1
C: 10−16=−6; flip and add 1 gives 0110, so −6
D: −8
Negation in Two's Complement
001111001101−30011 + 1101 = 1 0000. Discard the carry outside four bits, leaving 0000.
The same procedure converts a negative number back to its positive counterpart.
Negation Practice and Why the Method Works
For each four-bit pattern, flip every bit and add 1. State the original and resulting decimal values.
0101111010011000Proof question: For an n-bit value x, prove that flipping every bit and adding 1 represents −x.
A: +5 → 1011 = −5
B: −2 → 0010 = +2
C: −7 → 0111 = +7
D: −8 → 1000; +8 is outside the four-bit signed range
Flipping all n bits changes x to (2n−1)−x. Adding 1 gives 2n−x.
Keeping only n bits is arithmetic modulo 2n. Thus 2n−x ≡ −x, so the result is the n-bit representation of the negative.
Addition in Two's Complement
111011011011−5The full sum is 1 1011. Discard the carry outside the 4-bit word; 1011 represents −5.
No separate “negative-number adder” is required.
Add Signed Numbers and Verify the Result
Represent each operand in four bits, add the patterns, discard any carry out, and convert the stored result back to decimal.
A: 1110+1101=1011 = −5
B: 0101+1101=0010 = 2
C: 1100+0110=0010 = 2
D: stored 0111 = 7, but −9 is outside the range: signed overflow
Subtraction in Two's Complement
To compute a − b, negate b using two's complement and add: a − b = a + (−b).
5 − 30101 − 00110011 → 11010101 + 1101 = 00102The carry outside the four-bit word is discarded, exactly as in addition.
Subtraction Practice
Use four-bit two's complement. Rewrite each subtraction as addition of a negated number, compute the stored result, and verify the decimal answer.
A: 0101+1101=0010 = 2
B: 0010+1010=1100 = −4
C: 1101+1110=1011 = −5
D: 1010+0101=1111 = −1
Binary Representation, Addition, and Subtraction
n bits provide 2n distinct patterns.
Add from the LSB and propagate carry toward the MSB.
Two's complement gives the range −2n−1 through 2n−1−1.
Negate the second operand, then add.
- The same adder handles signed and unsigned bit patterns.
- There is only one representation of zero.
- Subtraction can be performed by adding the negated operand.
Lecture 6: Adders and the Hack ALU
How can gates implement the binary addition procedure developed in this lecture?