Introduction
Unsigned binary encodes only non-negative integers. Processors also need negative values. Three classical encodings place a sign into a fixed-width bit pattern: sign-magnitude, ones' complement, and two's complement. Modern CPUs almost universally use two's complement because addition and subtraction use the same adder hardware and because it has a single zero.
This note fixes the encodings, their ranges, negation rules, and the overflow condition for two's-complement addition. Arithmetic circuits that implement them appear in Adders and Subtractors and Fixed-Point Arithmetic and Overflow.
Prerequisites
- Number Systems and Base Conversion: binary place values, bit width, hex shorthand.
Core concepts
Fixed width
All statements assume an -bit word. Patterns are written with the MSB on the left. When a signed encoding is in force, the MSB is the sign bit (0 for non-negative, 1 for negative) in every scheme below, but the meaning of the remaining bits differs.
Sign-magnitude
- Value: , where is the MSB and is the magnitude in the remaining bits.
- Range: to .
- Two zeros: and .
Example (): is .
Ones' complement
- Positive numbers match unsigned.
- The negative of is obtained by bitwise complement (flip every bit).
- Range: to .
- Two zeros: and .
Example (): , so .
Two's complement
- Positive numbers match unsigned.
- The negative of is (mod ), equivalently for .
- Range: to . One extra negative value; a single zero .
- Decoding: if MSB is 0, read as unsigned; if MSB is 1, the value is .
Example (): . Decode: .
Weighted-sign view. A two's-complement pattern has value
so the MSB carries weight and every other bit its usual positive weight. Example: . The same view for ones' complement gives the MSB weight : .
Shortcut for negation. Scanning from the LSB, copy every bit up to and including the first 1, then invert all bits to its left. For : copy , invert the rest, giving .
Excess (biased) code
An -bit excess- code stores as the unsigned pattern . The usual bias is : in 4-bit excess-8, , and . Excess- is two's complement with the MSB inverted, so its patterns sort in the same order as their values. IEEE 754 exponents use a bias of instead (127 for 8 bits), see Floating-Point Representation (IEEE 754).
One pattern, four readings
| 4-bit pattern | Unsigned | Sign-magnitude | Ones' complement | Two's complement |
|---|---|---|---|---|
| 0111 | 7 | |||
| 1000 | 8 | |||
| 1011 | 11 | |||
| 1111 | 15 |
Different count: negative values versus patterns with sign bit 1. In every scheme patterns have MSB 1. In two's complement all of them are negative values; in sign-magnitude and ones' complement one of them is , so only distinct negative values exist.
| Property | Sign-magnitude | Ones' complement | Two's complement |
|---|---|---|---|
| Negation | Flip sign bit | Bitwise NOT | NOT then add 1 |
| Zeros | two | two | one |
| Range (n bits) | |||
| Adder hardware | needs sign logic | end-around carry | ordinary adder |

How it works
Negation in two's complement
- Write the positive magnitude as an -bit pattern (pad with leading zeros).
- Invert every bit.
- Add 1, discarding a carry out of the MSB.
Edge case. Negating (the pattern ) overflows the representable range: there is no positive counterpart in bits. Hardware that must reject this case raises an overflow flag on negation.
Addition in two's complement
Add the patterns as unsigned integers mod . The same adder works for two positives, two negatives, or mixed signs. Interpretation of the result uses the two's-complement decode rule above.
Example (). : (carry out discarded), value .
Overflow
Overflow means the true mathematical sum lies outside the representable range. Equivalently, for two's complement:
- Adding two positives yields a negative pattern, or
- Adding two negatives yields a non-negative pattern.
Mixed-sign addition never overflows in two's complement, because the magnitude of the sum is at most the larger magnitude, which already fit.
A circuit test: number the carries so that is the carry into the sign position (out of bit ) and the carry out of it. Then overflow .
Why the XOR works. Let the sign bits be and the sum bit . If , then , and overflow (a negative result) happens exactly when . If , then , and overflow (a non-negative result) happens exactly when . If , then and . In all cases .
Carry versus overflow: the four flag combinations
The carry flag reports unsigned wrap-around; reports signed overflow. They are independent. In 4 bits:
| Operation | Patterns | Result | Unsigned reading | Signed reading | ||
|---|---|---|---|---|---|---|
| 0 | 0 | , correct | , correct | |||
| 0 | 1 | , correct | , wrong | |||
| 1 | 0 | wraps to 2 | , correct | |||
| 1 | 1 | wraps to 7 | , wrong |
Subtraction
is computed as : invert and set the adder's carry-in to 1. Signed overflow in subtraction happens exactly when and have different signs and the result's sign differs from 's.
- : . Carry out 0, which in this convention means an unsigned borrow (); no signed overflow.
- : . Kept bits : a negative minus a positive gave a positive, so .
Two's-complement addition example (). : , which decodes as . Both operands were positive; the result is negative: overflow. Ordinary binary addition does not implement sign-magnitude arithmetic; use the separate sign/magnitude rules there.
Ones' complement and sign-magnitude addition
Ones' complement. Add the patterns; if a carry leaves the MSB, add it back into the LSB (end-around carry).
- in 4 bits: ; end-around carry gives .
- : ; end-around gives .
- : , no carry; the result is . Hardware must treat as equal to .
Sign-magnitude. If the signs agree, add the magnitudes and keep the sign. If they differ, subtract the smaller magnitude from the larger and take the sign of the larger. This needs a magnitude comparator, which is the reason the scheme is not used for integers.
Algorithms and pseudocode
Pseudocode treats patterns as integers in .
TO-TWOS(x, n) // mathematical integer x
1 M <- 2^n
2 if x >= 0 then return x mod M
3 else return (M + x) mod M
FROM-TWOS(p, n) // pattern p
1 if p < 2^(n-1) then return p
2 else return p - 2^n
NEGATE-TWOS(p, n)
1 return ((bitwise_not n bits of p) + 1) mod 2^n
ADD-TWOS(a, b, n)
1 s <- (a + b) mod 2^n
2 overflow <- (sign(a)=sign(b)) and (sign(s)!=sign(a))
3 return (s, overflow)Sign of a pattern is its MSB. Best, average and worst case for these operations on an -bit word are all gate delays in a ripple implementation, because the circuit is the same whatever the data; see Adders and Subtractors for carry-lookahead.
Mathematical explanation
Two's complement identifies the ring with signed representatives in . Addition mod therefore coincides with signed addition whenever the mathematical sum lies in that interval. When it does not, the representative still exists in the ring, but it is not the mathematical sum: that mismatch is overflow.
Ones' complement addition requires an end-around carry: a carry out of the MSB is added back into the LSB. That extra step is why two's complement replaced it in ALUs.
Worked examples
Example 1: encode in 8 bits
| Encoding | Pattern |
|---|---|
| Sign-magnitude | |
| Ones' complement | |
| Two's complement |
Example 2: 4-bit two's complement table (selected)
| Pattern | Value | Pattern | Value |
|---|---|---|---|
| 0000 | 0 | 1000 | |
| 0001 | 1001 | ||
| 0111 | 1111 |
Example 3: and in 4-bit two's complement
- (value 2); signs differ; no overflow.
- (value ); both positive, result negative; overflow.
Example 4: negate 22 in 8 bits
. Invert ; add 1 . Decode: .
Example 5: encode in 8 bits, all three ways
. Sign-magnitude sets the MSB: . Ones' complement inverts every bit: . Two's complement adds 1: . Check with the weighted-sign view: .
Example 6: the 8-bit pattern
Unsigned 128; sign-magnitude ; ones' complement ; two's complement . Negating it in two's complement returns the same pattern () with .
Example 7: sign extension and arithmetic shift
- in 4 bits is ; sign-extended to 8 bits it is , still . Zero-extension would give .
- Why sign extension is exact. Adding a new top bit of weight and turning the old sign bit's weight from into changes the value by .
- Arithmetic right shift divides by 2 and rounds toward : , which is , not the truncated that integer division in C gives.
Important properties and observations
- in -bit two's complement is always the all-ones pattern.
- Arithmetic right shift of a two's-complement number preserves the sign (copies the MSB); logical right shift inserts zeros and is for unsigned data.
- Sign extension: to widen an -bit two's-complement value to bits, copy the sign bit into the new high bits. Zero extension is correct for unsigned values and nonnegative two's-complement values; negative two's-complement values require sign extension.
- Comparison: two's-complement comparison is not the same as unsigned comparison when the MSB differs; hardware uses a signed comparator or subtract-and-test-overflow path.
Common misconceptions.
- "Two's complement range is symmetric." It is not: one more negative value.
- "Overflow is the carry out." Carry out alone is for unsigned wrap; signed overflow is carry into XOR carry out of the sign.
- "Ones' complement and two's complement represent the same set of values." They do not: only the non-negative encodings coincide. Ones' complement has a second zero (all ones) and stops at ; two's complement has one zero and reaches .
- "A carry out means the signed result is wrong." produces a carry out and a correct signed result; produces no carry out and a wrong one.
- "Arithmetic shift right is the same as integer division by 2." Only for non-negative values; for negative odd values it rounds down, not toward zero.
GATE examination perspective
- ENCODE AND DECODE. Convert a given decimal to each of the three encodings, or decode a bit string under a stated encoding.
- OVERFLOW TRAPS. Decide whether a given addition overflows; do not confuse carry out with signed overflow.
- RANGE QUESTIONS. For bits, two's minimum is , maximum .
- NEGATION EDGE. Negating in two's complement has no representable result.
- WHICH ZERO. Count distinct zeros under each encoding (2, 2, 1).
- FLAG TABLES. Give both and for an addition; they are independent, and the carry-in XOR carry-out rule gives .
- SUBTRACTION OVERFLOW. Operands of different signs, result sign different from the minuend: overflow. Same-sign operands never overflow in subtraction.
- SAME BITS, DIFFERENT SCHEME. Decode one pattern under unsigned, sign-magnitude, ones' and two's complement; the weighted-sign view is the fastest method.
- SIGN EXTENSION AND SHIFTS. Copy the MSB when widening; arithmetic right shift rounds toward .
Summary
Sources
- M. Morris Mano, M. D. Ciletti, Digital Design, 5th ed., Pearson, 2013: Chapter 1, Sections 1.5 and 1.6 (complements of numbers; signed binary numbers).