Signed Integer Representation (Twoโs Complement)
Context: FIT1047_MOC ยท three schemes for negative numbers ยท why hardware chose 2โs complement ยท overflow detection by sign pattern
Quick Revision
- ๐ฏ Objective: negate in 2โs complement โ flip all bits, add 1 โ regular long addition then works for signed arithmetic.
- ๐ฆ Core Components: sign-magnitude (two zeros) โ 1โs complement (flip only, still two zeros) โ 2โs complement (flip+1, one zero, easy hardware).
- โก Key Constraint: overflow rule โ same-sign operands producing an opposite-sign result โน overflow; the discarded carry bit is NOT the test.
๐ Core
1. Sign-and-Magnitude
- Scheme โ leftmost bit = sign, rest = magnitude: .
- Drawbacks โ two zeros ( and ); addition/subtraction and overflow detection hard to implement.
2. 1โs Complement
- Scheme โ negative = flip every bit: (3-bit).
- Improvement โ easier arithmetic than sign-magnitude, but still two zeros (, ).
3. 2โs Complement (the one computers use)
- Negation recipe โ flip all bits, then : .
- Landmarks (n bits) โ ยท smallest ยท largest ยท exactly one zero ยท leftmost bit still signals sign.
- Why hardware loves it โ regular long addition works unchanged; overflow detected by sign pattern; no duplicate zero.
- Range asymmetry โ 3 bits: โ one more negative than positive value.
๐ Exam Execution Trace โ overflow detection (3-bit, from lecture)
| Sum | Bits | Result | Signs | Verdict |
|---|---|---|---|---|
| pos+pos โ neg | OVERFLOW | |||
| (carry discarded) | neg+neg โ pos | OVERFLOW | ||
| (carry discarded) | neg+neg โ neg | ok โ | ||
| Rule: overflow โบ two same-sign operands yield an opposite-sign result; mixed-sign addition can never overflow. |
โ๏ธ Core Decision Matrix
| Scheme | Negation | Zeros | Arithmetic | Range (n bits) |
|---|---|---|---|---|
| sign-magnitude | flip sign bit | two | hard | |
| 1โs complement | flip all bits | two | medium | |
| 2โs complement | flip + add 1 | one | long addition works |
โ๏ธ Practice
Using 4 bits: (a) write in 2's complement; (b) compute and state whether it overflows; (c) compute and state whether it overflows.
Answer
- (a) flip .
- (b) โ no overflow (mixed signs never overflow).
- (c) โ overflow (pos+pos โ neg; ).
- Key move: verdict comes from the sign pattern, not from any carry.
โ ๏ธ Common Mistakes
- ๐ก Discarded carry โ overflow โ discards a carry yet is correct; only the sign-pattern rule decides.
- ๐ก Flip+1 is an involution โ applying it twice returns the original โ use that to decode a negative pattern back to magnitude.
- ๐ก has no positive partner โ negating overflows; the range is asymmetric by one.