Signed Integers & Two’s Complement: Concept, Negation, Arithmetic, Ranges
Signed Integers & Two’s Complement: Concept, Negation, Arithmetic, Ranges
This page explains in depth how signed integers are represented in two’s complement form, how to perform calculations with them, and how to interpret value ranges.
Concept of two’s complement
Two’s complement is the standard system used by modern computers to represent signed integers. It allows addition and subtraction to work identically for positive and negative numbers.
The rule: −X = 2ⁿ − X, where n is the bit width.
Example (8-bit):
- +15 = 00001111₂
- −15 = 11110001₂ (because 2⁸ − 15 = 241)
The system behaves like a circle. After reaching the maximum value (127), adding 1 wraps around to −128.
How to negate a value
To compute the negative of a number (its two’s complement):
- Invert all bits.
- Add 1 to the result.
Example: −20
- 20 = 00000000 00010100₂
- Invert bits → 11111111 11101011₂
- Add 1 → 11111111 11101100₂ = 0xFFEC
This method works both ways. Negating a negative number gives back the positive.
Arithmetic in two’s complement
Addition and subtraction use the same hardware as for unsigned integers.
Example 1: 15 + (−5)
- 15 = 00001111₂
- −5 = 11111011₂
- Sum = 00001010₂ = 10₁₀
✓ Works correctly.
Example 2: −10 + (−3)
- −10 = 11110110₂
- −3 = 11111101₂
- Sum = 11110011₂ = −13
Overflow occurs only when the sign of the result differs from both operands.
Value ranges
For n bits:
Type | Range
--------------------------|--------------------------
Unsigned | 0 to 2ⁿ − 1
Signed (two’s complement) | −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1
Examples:
- 8-bit signed: −128 to +127
- 16-bit signed: −32 768 to +32 767
- 32-bit signed: −2 147 483 648 to +2 147 483 647
Why it works
In two’s complement, subtraction is just addition with the negated value. The carry beyond the MSB is ignored, which restores the correct result.
Example: 00000101₂ (5) + 11111101₂ (−3) = 1 00000010₂ → ignore carry → 00000010₂ = 2₁₀.
Bit interpretation
The same 8-bit pattern can represent different numbers:
Pattern | Unsigned | Signed
-------- | -------- | -------
00000000 | 0 | 0
01111111 | 127 | 127
10000000 | 128 | −128
11111111 | 255 | −1
The CPU does not know whether the data is signed or unsigned — it is the programmer or data type that defines it.
Overflow and wrapping
Adding 1 to the maximum value wraps around:
- 127 + 1 = −128 (in 8-bit signed)
- 255 + 1 = 0 (in 8-bit unsigned)
This cyclical property is important for low-level programming and bitwise logic.
Summary
- Two’s complement unifies addition and subtraction for signed numbers.
- It avoids the “two zeros” problem of one’s complement.
- The highest bit serves as the sign bit but participates fully in arithmetic.
- Every integer operation is modular over 2ⁿ.