3.1.3Boolean Algebra & Logic Gates

Two's complement signed numbers

1,762 words8 min readdifficulty · medium6 backlinks

WHAT is two's complement?

  • Range for nn bits:   [2n1, 2n11]\;[-2^{n-1},\ 2^{n-1}-1].
  • Example (8-bit): [128,+127][-128, +127]. Note it's asymmetric — one more negative than positive.

WHY does the weird "negative weight" work?

Formally: unsigned value of the bits is U=bn12n1+i<n1bi2iU = b_{n-1}2^{n-1} + \sum_{i<n-1} b_i 2^i. We want negatives when the top bit is set, and we want arithmetic mod 2n2^n. So define the signed value: VU(mod2n),V[2n1,2n11]V \equiv U \pmod{2^n},\quad V \in [-2^{n-1}, 2^{n-1}-1] When bn1=1b_{n-1}=1:   V=U2n=(2n1+rest)2n=2n1+rest\;V = U - 2^n = \big(2^{n-1} + \text{rest}\big) - 2^n = -2^{n-1} + \text{rest} — exactly the definition's negative weight. WHY: subtracting 2n2^n is invisible to an nn-bit machine (it just drops off the top), so the hardware never needs to know whether we "meant" UU or VV.


HOW to negate a number (find x-x)


Worked examples

Figure — Two's complement signed numbers

Overflow — the one thing carries can't tell you


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a clock with 16 hours (0–15) instead of 12. Moving forward is like adding; going back one hour from 0 lands on 15. So on this clock, "15" is really "one step back" = 1-1. We just agree that the numbers on the far side of the clock (8–15) are the "negative" ones. To find the negative of a number, walk it the other way around the clock — and the quick trick to do that is: flip all the switches (0↔1) and add one. That's two's complement!


What weight does the MSB carry in n-bit two's complement?
2n1-2^{n-1} (a negative weight, not just a sign flag)
Range of an n-bit two's complement number?
[2n1, 2n11][-2^{n-1},\ 2^{n-1}-1]
Range of an 8-bit two's complement number?
[128, +127][-128,\ +127]
How do you compute x-x in two's complement?
Invert all bits then add 1 (because xˉ+1=2nxx\bar x + 1 = 2^n - x \equiv -x)
Why does invert-and-add-1 give the negative?
x+xˉ=2n1x+\bar x = 2^n-1, so xˉ+1=2nxx(mod2n)\bar x+1 = 2^n - x \equiv -x \pmod{2^n}
What do you do with the carry-out in signed addition?
Discard it; it equals 2n0(mod2n)2^n \equiv 0 \pmod{2^n}
How is signed overflow detected?
Carry into MSB \neq carry out of MSB, i.e. Cn1Cn=1C_{n-1}\oplus C_n = 1
Decode 11111011 (8-bit two's complement).
128+123=5-128+123 = -5
Why is the range asymmetric?
Single zero code frees one extra pattern for negatives, giving one more negative than positive
Negating 128-128 in 8-bit gives?
128-128 again (overflow — it has no positive counterpart)

Connections

  • Binary number system — unsigned foundation this builds on
  • Sign-magnitude representation — the naive alternative (two zeros, symmetric)
  • One's complement — the intermediate step (invert only, no +1)
  • Full adder — the circuit that "just works" for signed add/subtract
  • Modular arithmetic — why "mod 2n2^n" makes the carry vanish
  • Overflow and carry flags — status bits in the ALU

Concept Map

motivates

redefines

defines

bounds

is

justifies

1111 behaves as

rearranged gives

used in

used in

enables

No minus sign wire

Two's complement

MSB negative weight -2^n-1

V = -b*2^n-1 + rest

Range asymmetric -2^n-1 to 2^n-1 -1

Sign bit: 1 means negative

Odometer wrap-around mod 2^n

-1

x + NOT x = 2^n - 1

-x = NOT x + 1

Encode: invert then add 1

Decode: sum weights

One adder for add and subtract

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, computer ke paas sirf 0 aur 1 hote hain — koi alag se "minus" ka wire nahi hota. Toh negative numbers store karne ke liye humne ek jugaad banaya: two's complement. Isme sabse top wale bit (MSB) ka weight negative maan lete hain, yaani 2n1-2^{n-1}. Baaki bits normal positive weights. Isi ek chhoti si redefinition se saare negative numbers aa jaate hain, aur — sabse important — ek hi adder circuit se addition aur subtraction dono ho jaate hain. Yehi iska asli faayda hai.

Negative nikaalne ka trick simple hai: "flip karo, phir 1 add karo". Har bit ulta karo (0↔1), phir 1 jodo. Iska proof bhi easy hai: x+xˉ=x + \bar x = saare 1 =2n1= 2^n - 1, toh xˉ+1=2nx\bar x + 1 = 2^n - x, aur 2nx2^n - x mod 2n2^n ka matlab hai x-x. Bas! Isliye ratne ki zaroorat nahi, derive kar sakte ho.

Subtraction ka jugaad: 757 - 5 karna hai toh 7+(5)7 + (-5) kar do. 5-5 ka two's complement banao, add karo, aur jo extra carry top se bahar aata hai use discard kar do — kyunki woh 2n2^n ke barabar hai jo nn bits me invisible hai (mod 2n2^n me zero). Yeh galat nahi, bilkul exact hai.

Ek cheez yaad rakho: range asymmetric hoti hai — 8-bit me 128-128 se +127+127. Ek zyada negative hota hai kyunki zero ka sirf ek hi code hota hai. Aur overflow tab hota hai jab do same-sign numbers add karne pe answer opposite sign ka aa jaaye — detection ka formula: MSB me aane wali carry aur bahar jaane wali carry alag ho (Cn1Cn=1C_{n-1} \oplus C_n = 1). Exam me yeh points aksar aate hain, dhyaan rakhna!

Go deeper — visual, from zero

Test yourself — Boolean Algebra & Logic Gates

Connections