Two’s Complement Basics
In two’s complement for an n-bit signed integer:
- Range: -2^(n-1) to 2^(n-1) - 1 (note the asymmetry)
- The Most Significant Bit (MSB) is the sign bit:
- 0 = positive (or zero)
- 1 = negative
- The Negation Rule: Geting the negative of a number:
- Step 1: nvert all bits (NOT operation: flip 0→1, 1→0)
- Step 2: Add 1
Two’s Complement Formula
In two’s complement, the MSB has a negative weight:
- MSB = 1 contributes -2³¹ to the total value
- All other bits contribute their positive powers of 2
For a 32-bit number, the value is:
| |
Simple Example (4-bit):
| |