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)

SumBitsResultSignsVerdict
pos+pos โ†’ negOVERFLOW
(carry discarded)neg+neg โ†’ posOVERFLOW
(carry discarded)neg+neg โ†’ negok โœ“
Rule: overflow โŸบ two same-sign operands yield an opposite-sign result; mixed-sign addition can never overflow.

โš–๏ธ Core Decision Matrix

SchemeNegationZerosArithmeticRange (n bits)
sign-magnitudeflip sign bittwohard
1โ€™s complementflip all bitstwomedium
2โ€™s complementflip + add 1onelong addition works

โœ๏ธ Practice

โš ๏ธ 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.