Exercises — Two's complement signed numbers
A tiny reminder of the two facts we lean on constantly:
Level 1 — Recognition
(Just read the bits. No arithmetic tricks yet.)
L1.1 Is the 8-bit pattern 10011010 positive or negative? What is the weight of its most significant bit?
Recall Solution — L1.1
The most significant bit (leftmost, ) is 1. In two's complement a 1 in the top position means the number is negative. Its weight is not but .
Answer: negative; MSB weight .
L1.2 For a 6-bit two's complement number, write down the exact representable range.
Recall Solution — L1.2
Range for bits is . Here , so . Answer: .
L1.3 Which of these 8-bit patterns represent negative numbers? 00000000, 01111111, 10000000, 11111111.
Recall Solution — L1.3
Look only at the top bit.
00000000→ top bit 0 → non-negative (it is ).01111111→ top bit 0 → non-negative ().10000000→ top bit 1 → negative ().11111111→ top bit 1 → negative (). Answer: the last two are negative.
Level 2 — Application
(Turn the crank: encode, decode, negate.)
L2.1 Encode in 8-bit two's complement.
Recall Solution — L2.1
- Write :
00010011. Why: start from the known positive. - Flip every bit:
11101100. Why: . - Add 1:
11101101. Why: . Check by weights: . ✓ Answer:11101101.
L2.2 Decode the 8-bit pattern 11001101.
Recall Solution — L2.2
Top bit is 1 → negative, weight . Add the positive weights of the remaining 1s:
positions set below the top are → .
.
Shortcut check: negate (flip+1): 11001101 → flip 00110010 → +1 00110011 . So original . ✓
Answer: .
L2.3 Compute in 8-bit two's complement by adding the negative.
Recall Solution — L2.3
00001100. Encode : 00010100 → flip 11101011 → +1 11101100.
Add:
No carry drops off the top here. Decode 11111000: .
Answer: (as expected, ). ✓
Level 3 — Analysis
(Now explain why things behave — especially the edge cases.)
L3.1 Negate in 8-bit two's complement (10000000). What do you get, and why?
Recall Solution — L3.1
Flip 10000000 → 01111111; add 1 → 10000000. We are back where we started.
Why: has no positive twin. Its true negation would be , but is outside the range , so it wraps. Formally .
Answer: 10000000 — negation overflows to itself.
L3.2 Add in 8-bit signed. Is the unsigned carry-out set? Is there signed overflow? Are those the same thing?
Recall Solution — L3.2
01100100, 00101101.
Carry-out (9th bit): none — the sum fits in 8 bits, so .
Signed value of 10010001: . Two positives gave a negative → signed overflow ().
Check the flag rule: carry into MSB (needed to make the top bit flip), carry out , so overflow. ✓
Answer: carry-out , but signed overflow . They are different flags — see Overflow and carry flags.
L3.3 Show that in 8-bit, the patterns 01111111 and 10000000 are the two range endpoints, and that adding 1 to each is the "danger" moment.
Recall Solution — L3.3
01111111 → the largest positive.
10000000 → the most negative.
Add 1 to 01111111: → 10000000 = . Two positives () wrapping to negative → overflow.
Add 1 to 11111111 (): → 00000000 = — that one is fine (, in range).
Answer: endpoints are and ; incrementing overflows, incrementing does not.
Level 4 — Synthesis
(Combine several ideas into one problem.)
L4.1 A CPU computes 11110110 + 00011001 in 8-bit signed. Give: (a) the raw 9-bit sum, (b) whether the carry-out is discarded, (c) the final signed value, (d) whether there is overflow.
Recall Solution — L4.1
Decode inputs first: 11110110 ; 00011001 .
Add the bytes:
(a) Raw 9-bit result 1 00001111.
(b) Carry-out — discarded, because it is worth .
(c) Remaining 8 bits 00001111 . And indeed . ✓
(d) Overflow? Operands have opposite signs ( and ), so overflow is impossible — a negative plus a positive always lands inside the range. Check flags: carry into MSB , carry out , → no overflow. ✓
Answer: (a) 1 00001111, (b) yes discarded, (c) , (d) no overflow.
L4.2 Using 4-bit two's complement, list every pattern and its value, then confirm the "one more negative than positive" claim.
Recall Solution — L4.2
Top bit has weight ; the rest are .
| bits | value | bits | value | |
|---|---|---|---|---|
| 0000 | 0 | 1000 | −8 | |
| 0001 | 1 | 1001 | −7 | |
| 0010 | 2 | 1010 | −6 | |
| 0011 | 3 | 1011 | −5 | |
| 0100 | 4 | 1100 | −4 | |
| 0101 | 5 | 1101 | −3 | |
| 0110 | 6 | 1110 | −2 | |
| 0111 | 7 | 1111 | −1 |
Negatives: through = 8 values. Positives: through = 7 values. Plus one zero. Total . ✓ Answer: range ; exactly one more negative () than positive, because the single zero code frees one extra pattern for the negatives.
Level 5 — Mastery
(Prove and generalise.)
L5.1 Prove the negation rule from the single fact . Then explain in one line why it fails to give a valid same-width result for exactly one input.
Recall Solution — L5.1
Proof. For any -bit , each bit position contributes a 1 to either or but never both, so bit-by-bit is all ones:
Rearrange: , hence
> Working mod $2^n$, , so . Therefore .
The failing input: (e.g. for ). Then , which is out of range ; the formula wraps it back to itself. Every other value has a valid negation.
L5.2 Design a subtraction: show that computing as needs no separate subtractor — only the Full adder chain plus one control bit. Demonstrate on in 8-bit.
Recall Solution — L5.2
Construction. From L5.1, . So
Feed into the adder and set the very first carry-in (that supplies the "" for free). A SUB control line can both invert and set ; when it is 0, passes straight through and , giving plain addition. One adder, two modes.
Demonstration : 00000101, 00001000, 11110111, .
Drop the carry-out (, invisible). Result 11111101. Decode: . And . ✓
Answer: the identity turns subtraction into "invert , carry-in 1," so the same adder does both; .
L5.3 Prove the overflow rule "" for the same-sign case by a counting argument: why must two positives that overflow produce a 1 in the top bit?
Recall Solution — L5.3
Take two positives , so both have top bit 0. Their true sum lies in .
- No overflow case: . Then still fits with top bit
0. No carry crosses into the MSB from the lower bits large enough to flip it, so ; and no carry leaves the top, . XOR . ✓ - Overflow case: . To reach a sum from two numbers whose top bits are
0, the lower bits must produce a carry into the MSB: . That carry sets the top bit to1(a negative-looking result), while nothing carries out since : . XOR . ✓ By symmetry, two negatives overflow with , again XOR . Opposite-sign operands can never push outside the range, so they never trigger it. Conclusion: overflow .
Connections
- Binary number system — the unsigned weights every problem starts from
- One's complement — the "flip only" step, why it is one short
- Modular arithmetic — why dropping the carry-out is exact
- Full adder — the circuit L5.2 reuses for subtraction
- Overflow and carry flags — the two different flags L3 and L4 separate