SEN2201 · Week 2 · Bits, Data Types, and Operations

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.

bitunsigned integersign-magnitude1's complement2's complementoverflowcarry-outsign extensionhexadecimalMSBLSB

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:

  1. The pattern carries no meaning, the representation does. 1100 0110 is 198 as unsigned and -58 as 2’s complement.
  2. In 2’s complement the weight of the MSB is negative: -128 in 8 bits.
  3. -X = invert + 1.
  4. Overflow occurs only when two numbers with the same sign are added and the result has the opposite sign.
  5. Sign extension: when widening, copy the MSB, do not fill with 0.

Bits and positional value

With kk bits you can write 2k2^k 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.

hex0123456789ABCDEF
bin0000000100100011010001010110011110001001101010111100110111101111
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.

RepresentationValue if MSB = 18-bit rangeZero
Unsignedadd normally0 … 2551
Sign-magnitudeminus, the value of the remaining 7 bits-127 … +1272 (+0, -0)
1’s complementflip all the bits, put a minus-127 … +1272
2’s complementcount the MSB as -128, add the rest-128 … +1271

For general kk bits:

2’s complement: −2k−1  …  2k−1−1SM and 1’s: −(2k−1−1)  …  2k−1−1\text{2's complement: } -2^{k-1} \;\ldots\; 2^{k-1}-1 \qquad \text{SM and 1's: } -(2^{k-1}-1) \;\ldots\; 2^{k-1}-1

Example: 0x9C = 1001 1100.

ReadingCalculationValue
Unsigned128+16+8+4156
Sign-magnitudeminus, 0011100 = 28-28
1’s complementflip: 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. 5+(−5)5 + (-5): 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 bitsValue8 bitsValue
01101+130000 1101+13
10110-101111 0110-10
10000-161111 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: A−B=A+(−B)A - B = A + (-B).

The overflow rule

If the result does not fit in the range of kk 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=10010110A = 10010110, B=00110010B = 00110010, A−BA - B = ?

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 −106−50=−156-106 - 50 = -156 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?

QuestionMethodExample
Unsigned NNthe smallest kk with 2k>N2^k > N300 → 29=5122^9 = 512 → 9 bits
2’s complement positive NN2k−1−1≥N2^{k-1} - 1 \ge N+100 → 27−1=1272^7 - 1 = 127 → 8 bits
2’s complement negative −N-N−2k−1≤−N-2^{k-1} \le -N-300 → −512-512 → 10 bits

Common mistakes

Wrong ideaThe truth
1000 0000 = -0 (2’s complement)-128. -0 exists in sign-magnitude
-X = just flip the bitsThat is 1’s complement; for 2’s add +1
If there is a carry-out there is overflowLook at the sign rule; the carry-out is discarded
(+) + (−) can overflowNever, the result lies between the two
In A − B look at the sign of BLook at the sign of -B
Sign extension: add 0s on the leftCopy the MSB
Group from the left for hexGroup 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.

Question 1easy

What range of integers can be represented with 6 bits in 2's complement?

Show answer
-32 to +31

The range of kk-bit 2's complement is from −2k−1-2^{k-1} to 2k−1−12^{k-1}-1. For k=6k = 6: −25=−32-2^5 = -32, 25−1=312^5 - 1 = 31.

Question 2easy

Convert 1101101011 (binary) to hexadecimal and to decimal (unsigned).

Show answer
0x36B = 875

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.

Question 3medium

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
211, -83, -44, -45

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.

Question 4easy

Represent -58 in 8-bit 2's complement. Give the answer in binary and hex.

Show answer
11000110 = 0xC6

+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.

Question 5easy

Sign-extend the 5-bit 2's complement value 10110 to 8 bits. What decimal value does it represent before and after?

Show answer
11110110, -10 both

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.

Question 6medium

Write -200 as a 16-bit 2's complement hexadecimal number.

Show answer
0xFF38

200 = 0000 0000 1100 1000 (0x00C8). Invert: 1111 1111 0011 0111. Add 1: 1111 1111 0011 1000 = 0xFF38.
Check: 0xFF38 = 65336, 65336 - 65536 = -200.

Question 7medium

Compute 01101100 + 00110001 in 8-bit 2's complement. Give the result and say whether overflow occurred.

Show answer
10011101, overflow

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.

Question 8medium

Compute 11110000 + 00100000 in 8-bit 2's complement. There is a carry out of bit 7. Is this an overflow?

Show answer
00010000 (+16), no overflow

-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.

Question 9medium

In 8-bit 2's complement, A = 00101100 and B = 11101001. Compute A - B as A + (-B). Did overflow occur?

Show answer
01000011 = 67, no overflow

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.

Question 10hard

In 8-bit 2's complement, A = 01011010 and B = 10100110. Compute A - B and decide whether overflow occurred.

Show answer
10110100, overflow

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.

Question 11hard

Compute 10111010 + 11000011 in 8-bit 2's complement and decide whether overflow occurred.

Show answer
01111101, overflow

-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.

Question 12hard

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
9 and 10

(a) 28=256≤300<512=292^8 = 256 \le 300 < 512 = 2^9 → 9 bits. (b) The smallest value of kk-bit 2's complement is −2k−1-2^{k-1}. −28=−256>−300-2^8 = -256 > -300 is not enough, −29=−512≤−300-2^9 = -512 \le -300 → 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).

Review card

k bits: unsigned 0..2^k-1 | SM and 1's -(2^(k-1)-1)..2^(k-1)-1 (two zeros) | 2's -2^(k-1)..2^(k-1)-1 8 bits: 0..255 | ±127 | -128..127 Read 2's: MSB = -128, the rest + (or unsigned - 256) -X: invert + 1 | shortcut: copy from the right up to the first 1, invert what is to its left SM: MSB is the sign, the rest is the magnitude | 1's: flip all, put a minus bin -> hex: group in fours from the RIGHT | hex -> bin: 4 bits per digit Sign extension: copy the MSB to the left (10110 -> 11110110, same value) OVERFLOW: two numbers with the same sign + result with the opposite sign (+)+(-) NEVER overflows. In A-B, look at the sign of -B! Hardware: carry into the MSB != carry out of the MSB Discard the 9th-bit carry. carry-out != overflow 2^10=1024 2^16=65536 -1 = 0xFF / 0xFFFF