Bits and integers: 2's complement, overflow, sign extension
Reading a bit pattern as unsigned, sign-magnitude, 1's and 2's complement, binary/hexadecimal conversions, 2's complement addition and subtraction and the overflow rule. The part of the midterm worth the most points.
This course starts with numbers, because a computer sees only bits: 0 or 1. An integer, a letter, an instruction are all the same string of 0s and 1s. The meaning depends on how you read that string. A question from this page is certain on the midterm: read a pattern in four ways, convert a number to 2’s complement, add two numbers and justify with the rule whether overflow occurred.
Golden rules:
- The pattern carries no meaning, the representation does. 1100 0110 is 198 as unsigned and -58 as 2’s complement.
- In 2’s complement the weight of the MSB is negative: -128 in 8 bits.
- -X = invert + 1.
- Overflow occurs only when two numbers with the same sign are added and the result has the opposite sign.
- Sign extension: when widening, copy the MSB, do not fill with 0.
Bits and positional value
With bits you can write different patterns. 8 bits: 256 patterns. In an unsigned integer the weight of each bit is a power of 2. The rightmost bit is the LSB (b0, weight 1), the leftmost is the MSB (b7 in 8 bits, weight 128).
weight: 128 64 32 16 8 4 2 1
bit: 1 0 0 1 1 1 0 1 = 128+16+8+4+1 = 157
Decimal → binary
There are two methods. Division: divide by 2, write the remainder, continue until the quotient is 0, read the remainders from bottom to top. Subtracting powers (faster): subtract the largest power of 2 that fits and write 1 in that column.
214 → 214-128=86 → 86-64=22 → 22-16=6 → 6-4=2 → 2-2=0
the 1s: 128, 64, 16, 4, 2 → 1101 0110 = 0xD6
Binary ↔ hexadecimal
One hex digit is exactly 4 bits. When going from binary to hex, group in fours from the right and complete the left group with 0s.
| hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| bin | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
10 1111 0111 → 0010 1111 0111 → 0x2F7
0x2F7 = 2·256 + 15·16 + 7 = 512 + 240 + 7 = 759
PITFALL · grouping from the left. For 1011101, if you write 1011|101 from the left you get B5, which is wrong. From the right: 101|1101 → 0101 1101 = 0x5D.
Signed numbers: four representations
There are three historical ways of writing negative numbers. In all of them, if MSB = 0 the number is positive and the value is the same as unsigned. The differences show up only when MSB = 1.
| Representation | Value if MSB = 1 | 8-bit range | Zero |
|---|---|---|---|
| Unsigned | add normally | 0 … 255 | 1 |
| Sign-magnitude | minus, the value of the remaining 7 bits | -127 … +127 | 2 (+0, -0) |
| 1’s complement | flip all the bits, put a minus | -127 … +127 | 2 |
| 2’s complement | count the MSB as -128, add the rest | -128 … +127 | 1 |
For general bits:
Example: 0x9C = 1001 1100.
| Reading | Calculation | Value |
|---|---|---|
| Unsigned | 128+16+8+4 | 156 |
| Sign-magnitude | minus, 0011100 = 28 | -28 |
| 1’s complement | flip: 0110 0011 = 99 | -99 |
| 2’s complement | -128+16+8+4 | -100 |
Quick check: 2’s complement value = unsigned - 256. 156 - 256 = -100.
Why does everyone use 2’s complement?
Because the adder circuit gives the right result without caring about the sign at all. : 0000 0101 + 1111 1011 = 1 0000 0000, and when the 9th bit is discarded, 0000 0000. There is a single zero and the adder is the same as for unsigned. The LC-3 and almost all computers use 2’s complement.
2’s complement: taking the negative
Recipe: write the positive, invert all the bits, add 1.
+77 = 0100 1101
invert = 1011 0010
+1 = 1011 0011 = -77 = 0xB3
check: -128 + 32 + 16 + 2 + 1 = -77
Shortcut: start from the right, copy as is up to the first 1 (including that 1), and invert every bit to its left. 0010 1000 (40) → 1101 1000 (-40).
Note: the most negative number has no positive counterpart. In 8 bits, -128 = 1000 0000; invert + 1 gives 1000 0000 again.
Sign extension
When you move a number into a wider field (for example a 5-bit offset into a 16-bit register), you copy the MSB to the left. If it is positive, 0s are added; if negative, 1s. The value does not change.
| 5 bits | Value | 8 bits | Value |
|---|---|---|---|
| 01101 | +13 | 0000 1101 | +13 |
| 10110 | -10 | 1111 0110 | -10 |
| 10000 | -16 | 1111 0000 | -16 |
In 16-bit hex, negative numbers start with F: -13 = 0xFFF3, -6 = 0xFFFA. When you write ADD R1, R1, #-1 in the LC-3, the 5-bit 11111 is widened to 16 bits this way (weeks 7-8).
PITFALL · filling with 0. If you turn 10110 into 00010110, it becomes 22. A negative number turned positive. In sign extension there is a single rule: copy the MSB.
Addition, subtraction and overflow
2’s complement addition is ordinary binary addition: from right to left, carry the carries upward. 1+1 = 0 carry 1, 1+1+1 = 1 carry 1. The 9th bit spilling out of the MSB is discarded.
There is no separate circuit for subtraction: .
The overflow rule
If the result does not fit in the range of bits, overflow occurs. The way to detect it is to look at the signs:
- If (+) + (+) = (−), overflow.
- If (−) + (−) = (+), overflow.
- In (+) + (−) overflow never occurs.
The exam sentence: “Overflow occurs only if both operands have the same sign and the result has the opposite sign.” The equivalent test in hardware: if the carry into the MSB differs from the carry out of the MSB, overflow.
Worked example 1: 01010101 + 00101010
0101 0101 ( 85)
+ 0010 1010 ( 42)
-----------
0111 1111 (127) two positives, positive result → no overflow
Worked example 2: , , = ?
A = 1001 0110 = -106
B = 0011 0010 = +50 → -B: invert 1100 1101, +1 → 1100 1110 (-50)
1001 0110 (-106)
+ 1100 1110 ( -50)
-----------
1 0110 0100 → the 9th bit is discarded → 0110 0100 = +100
The true answer does not fit in 8 bits. The two numbers added (A and -B) are negative, the result is positive: overflow. The carry into bit 7 is 0, the carry out is 1.
PITFALL · confusing carry-out with overflow. 11110000 + 00100000 = 1 00010000. A 1 spilled out but the result is 16, which is correct (-16 + 32). In unsigned addition, carry-out means “the result is large”; in 2’s complement it is not an indicator of overflow. The opposite also happens: in 01101100 + 00110001 there is no carry-out, but there is overflow.
PITFALL · looking at the wrong number in A − B. When applying the overflow rule, look at the sign of -B, not of B. Because that is the number being added.
How many bits are needed?
| Question | Method | Example |
|---|---|---|
| Unsigned | the smallest with | 300 → → 9 bits |
| 2’s complement positive | +100 → → 8 bits | |
| 2’s complement negative | -300 → → 10 bits |
Common mistakes
| Wrong idea | The truth |
|---|---|
| 1000 0000 = -0 (2’s complement) | -128. -0 exists in sign-magnitude |
| -X = just flip the bits | That is 1’s complement; for 2’s add +1 |
| If there is a carry-out there is overflow | Look at the sign rule; the carry-out is discarded |
| (+) + (−) can overflow | Never, the result lies between the two |
| In A − B look at the sign of B | Look at the sign of -B |
| Sign extension: add 0s on the left | Copy the MSB |
| Group from the left for hex | Group in fours from the right |
| 8-bit 2’s range is -127..127 | -128..127 (one zero, one extra negative) |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
What range of integers can be represented with 6 bits in 2's complement?
Show answer
The range of -bit 2's complement is from to . For : , .
Convert 1101101011 (binary) to hexadecimal and to decimal (unsigned).
Show answer
Group in fours from the right: 11 | 0110 | 1011 → 0011 0110 1011 = 3, 6, B → 0x36B.
Decimal: 3·256 + 6·16 + 11 = 768 + 96 + 11 = 875.
Give the decimal value of the 8-bit pattern 0xD3 as (a) unsigned, (b) sign-magnitude, (c) 1's complement, (d) 2's complement.
Show answer
0xD3 = 1101 0011. (a) 128+64+16+2+1 = 211.
(b) MSB = 1 → negative; the remaining 7 bits 1010011 = 83 → -83.
(c) Flip all the bits: 0010 1100 = 44 → -44.
(d) Count the MSB as -128: -128+64+16+2+1 = -45. Check: 211 - 256 = -45.
Represent -58 in 8-bit 2's complement. Give the answer in binary and hex.
Show answer
+58 = 32+16+8+2 = 0011 1010. Invert: 1100 0101. Add 1: 1100 0110.
Hex: 1100 = C, 0110 = 6 → 0xC6. Check: -128+64+4+2 = -58.
Sign-extend the 5-bit 2's complement value 10110 to 8 bits. What decimal value does it represent before and after?
Show answer
The MSB is 1, copy three 1s to the left: 11110110. Before: -16+4+2 = -10. After: -128+64+32+16+4+2 = -10. The value does not change. If you had added 0s on the left it would be 00010110 = 22, which is wrong.
Write -200 as a 16-bit 2's complement hexadecimal number.
Show answer
200 = 0000 0000 1100 1000 (0x00C8). Invert: 1111 1111 0011 0111. Add 1: 1111 1111 0011 1000 = 0xFF38.
Check: 0xFF38 = 65336, 65336 - 65536 = -200.
Compute 01101100 + 00110001 in 8-bit 2's complement. Give the result and say whether overflow occurred.
Show answer
108 + 49 = 157, but the largest positive number in 8 bits is 127. The sum comes out as 10011101, with MSB 1 (negative, -99). Two positives were added and the result is negative: overflow. The carry into bit 7 is 1, the carry out is 0, they differ.
Compute 11110000 + 00100000 in 8-bit 2's complement. There is a carry out of bit 7. Is this an overflow?
Show answer
-16 + 32 = 16. Adding two numbers with different signs can never overflow. The carry out of bit 7 is discarded; in 2's complement the carry-out is not an indicator of overflow. The result 00010000 = 16 is correct.
In 8-bit 2's complement, A = 00101100 and B = 11101001. Compute A - B as A + (-B). Did overflow occur?
Show answer
B = -23, -B = +23 = 00010111 (invert B: 00010110, +1: 00010111).
A + (-B) = 00101100 + 00010111 = 01000011 = 67. Check: 44 - (-23) = 67.
The two numbers added (A and -B) are positive, the result is positive → no overflow.
In 8-bit 2's complement, A = 01011010 and B = 10100110. Compute A - B and decide whether overflow occurred.
Show answer
A = 90, B = -90. -B = 01011010 (+90).
A + (-B) = 01011010 + 01011010 = 10110100 (MSB 1, read as -76).
The true result is 180 > 127. Two positives (A and -B) were added and the result is negative: overflow. Trap: do not say "different signs, no overflow" just because B is negative; the number whose sign you should look at is -B.
Compute 10111010 + 11000011 in 8-bit 2's complement and decide whether overflow occurred.
Show answer
-70 + (-61) = -131 < -128, it does not fit. Added bit by bit the sum is 1 01111101, and the 9th bit is discarded: 01111101 = +125. Two negatives were added and the result is positive: overflow. The carry into bit 7 is 0, the carry out is 1.
What is the minimum number of bits needed to represent (a) the unsigned value 300, (b) the value -300 in 2's complement?
Show answer
(a) → 9 bits. (b) The smallest value of -bit 2's complement is . is not enough, → 10 bits.
Verification: All conversions, the four-way reading tables, the addition results, and the carry and overflow decisions were computed bit by bit with node (verify/SEN2201/02.mjs).