Formally: unsigned value of the bits is U=bn−12n−1+∑i<n−1bi2i.
We want negatives when the top bit is set, and we want arithmetic mod 2n. So define the signed value:
V≡U(mod2n),V∈[−2n−1,2n−1−1]
When bn−1=1: V=U−2n=(2n−1+rest)−2n=−2n−1+rest — exactly the definition's negative weight. WHY: subtracting 2n is invisible to an n-bit machine (it just drops off the top), so the hardware never needs to know whether we "meant" U or V.
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. 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?
−2n−1 (a negative weight, not just a sign flag)
Range of an n-bit two's complement number?
[−2n−1,2n−1−1]
Range of an 8-bit two's complement number?
[−128,+127]
How do you compute −x in two's complement?
Invert all bits then add 1 (because xˉ+1=2n−x≡−x)
Why does invert-and-add-1 give the negative?
x+xˉ=2n−1, so xˉ+1=2n−x≡−x(mod2n)
What do you do with the carry-out in signed addition?
Discard it; it equals 2n≡0(mod2n)
How is signed overflow detected?
Carry into MSB = carry out of MSB, i.e. Cn−1⊕Cn=1
Decode 11111011 (8-bit two's complement).
−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 in 8-bit gives?
−128 again (overflow — it has no positive counterpart)
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 −2n−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ˉ= saare 1 =2n−1, toh xˉ+1=2n−x, aur 2n−x mod 2n ka matlab hai −x. Bas! Isliye ratne ki zaroorat nahi, derive kar sakte ho.
Subtraction ka jugaad: 7−5 karna hai toh 7+(−5) kar do. −5 ka two's complement banao, add karo, aur jo extra carry top se bahar aata hai use discard kar do — kyunki woh 2n ke barabar hai jo n bits me invisible hai (mod 2n me zero). Yeh galat nahi, bilkul exact hai.
Ek cheez yaad rakho: range asymmetric hoti hai — 8-bit me −128 se +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 (Cn−1⊕Cn=1). Exam me yeh points aksar aate hain, dhyaan rakhna!