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

Core concepts

Fixed width

All statements assume an nn-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: (−1)s⋅∣m∣(-1)^{s}\cdot |m|, where ss is the MSB and mm is the magnitude in the remaining n−1n-1 bits.
  • Range: −(2n−1−1)-(2^{n-1}-1) to +(2n−1−1)+(2^{n-1}-1).
  • Two zeros: +0=000…0+0 = 000\ldots 0 and −0=100…0-0 = 100\ldots 0.

Example (n=8n = 8): −5-5 is 1000010110000101.

Ones' complement

  • Positive numbers match unsigned.
  • The negative of xx is obtained by bitwise complement x‾\overline{x} (flip every bit).
  • Range: −(2n−1−1)-(2^{n-1}-1) to +(2n−1−1)+(2^{n-1}-1).
  • Two zeros: 000…0000\ldots 0 and 111…1111\ldots 1.

Example (n=8n = 8): +5=00000101+5 = 00000101, so −5=11111010-5 = 11111010.

Two's complement

  • Positive numbers match unsigned.
  • The negative of xx is x‾+1\overline{x}+1 (mod 2n2^n), equivalently 2n−x2^n - x for x≠0x \ne 0.
  • Range: −2n−1-2^{n-1} to +(2n−1−1)+(2^{n-1}-1). One extra negative value; a single zero 000…0000\ldots 0.
  • Decoding: if MSB is 0, read as unsigned; if MSB is 1, the value is unsigned(pattern)−2n\textit{unsigned}(pattern) - 2^n.

Example (n=8n = 8): −5=00000101‾+1=11111010+1=11111011-5 = \overline{00000101}+1 = 11111010+1 = 11111011. Decode: 251−256=−5251 - 256 = -5.

Weighted-sign view. A two's-complement pattern bn−1bn−2…b0b_{n-1}b_{n-2}\ldots b_0 has value

v=−bn−1 2n−1+∑i=0n−2bi 2i,v = -b_{n-1}\,2^{n-1} + \sum_{i=0}^{n-2} b_i\, 2^{i},

so the MSB carries weight −2n−1-2^{n-1} and every other bit its usual positive weight. Example: 1011=−8+2+1=−51011 = -8 + 2 + 1 = -5. The same view for ones' complement gives the MSB weight −(2n−1−1)-(2^{n-1} - 1): 1011=−7+3=−41011 = -7 + 3 = -4.

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 22=0001 011022 = 0001\,0110: copy 1010, invert the rest, giving 1110 10101110\,1010.

Excess (biased) code

An nn-bit excess-BB code stores vv as the unsigned pattern v+Bv + B. The usual bias is B=2n−1B = 2^{n-1}: in 4-bit excess-8, 0000=−80000 = -8, 1000=01000 = 0 and 1111=+71111 = +7. Excess-2n−12^{n-1} 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 2k−1−12^{k-1} - 1 instead (127 for 8 bits), see Floating-Point Representation (IEEE 754).

One pattern, four readings

4-bit patternUnsignedSign-magnitudeOnes' complementTwo's complement
01117+7+7+7+7+7+7
10008−0-0−7-7−8-8
101111−3-3−4-4−5-5
111115−7-7−0-0−1-1

Different count: negative values versus patterns with sign bit 1. In every scheme 2n−12^{n-1} patterns have MSB 1. In two's complement all of them are negative values; in sign-magnitude and ones' complement one of them is −0-0, so only 2n−1−12^{n-1} - 1 distinct negative values exist.

PropertySign-magnitudeOnes' complementTwo's complement
NegationFlip sign bitBitwise NOTNOT then add 1
Zerostwotwoone
Range (n bits)±(2n−1−1)\pm(2^{n-1}-1)±(2n−1−1)\pm(2^{n-1}-1)−2n−1..2n−1−1-2^{n-1}..2^{n-1}-1
Adder hardwareneeds sign logicend-around carryordinary adder
4-bit sign-magnitude, ones' complement and two's complement number wheels
Figure 1. Three 4-bit encodings arranged as number wheels. Two's complement places −8-8 opposite 00 and has no second zero; sign-magnitude and ones' complement each show two zeros.

How it works

Negation in two's complement

  1. Write the positive magnitude as an nn-bit pattern (pad with leading zeros).
  2. Invert every bit.
  3. Add 1, discarding a carry out of the MSB.

Edge case. Negating −2n−1-2^{n-1} (the pattern 100…0100\ldots 0) overflows the representable range: there is no positive counterpart in nn 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 2n2^n. 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 (n=4n = 4). 5+(−3)5 + (-3): 0101+1101=00100101 + 1101 = 0010 (carry out discarded), value +2+2.

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 cn−1c_{n-1} is the carry into the sign position (out of bit n−2n-2) and cnc_n the carry out of it. Then overflow V=cn⊕cn−1V = c_n \oplus c_{n-1}.

Why the XOR works. Let the sign bits be a,ba, b and the sum bit s=a⊕b⊕cn−1s = a \oplus b \oplus c_{n-1}. If a=b=0a = b = 0, then cn=0c_n = 0, and overflow (a negative result) happens exactly when cn−1=1c_{n-1} = 1. If a=b=1a = b = 1, then cn=1c_n = 1, and overflow (a non-negative result) happens exactly when cn−1=0c_{n-1} = 0. If a≠ba \ne b, then cn=cn−1c_n = c_{n-1} and V=0V = 0. In all cases V=cn⊕cn−1V = c_n \oplus c_{n-1}.

Carry versus overflow: the four flag combinations

The carry flag C=cnC = c_n reports unsigned wrap-around; VV reports signed overflow. They are independent. In 4 bits:

OperationPatternsResultCCVVUnsigned readingSigned reading
2+32 + 30010+00110010 + 001101010101002+3=52 + 3 = 5, correct55, correct
5+35 + 30101+00110101 + 0011100010000188, correct−8-8, wrong
5+(−3)5 + (-3)0101+11010101 + 110100100010105+13=185 + 13 = 18 wraps to 222, correct
(−3)+(−6)(-3) + (-6)1101+10101101 + 1010011101111113+10=2313 + 10 = 23 wraps to 777, wrong

Subtraction

A−BA - B is computed as A+B‾+1A + \overline{B} + 1: invert BB and set the adder's carry-in to 1. Signed overflow in subtraction happens exactly when AA and BB have different signs and the result's sign differs from AA's.

  • 3−53 - 5: 0011+1010+1=1110=−20011 + 1010 + 1 = 1110 = -2. Carry out 0, which in this convention means an unsigned borrow (3<53 < 5); no signed overflow.
  • (−8)−1(-8) - 1: 1000+1110+1=1 01111000 + 1110 + 1 = 1\,0111. Kept bits 0111=+70111 = +7: a negative minus a positive gave a positive, so V=1V = 1.

Two's-complement addition example (n=4n = 4). 5+65+6: 0101+0110=10110101+0110 = 1011, which decodes as −5-5. 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).

  • (−3)+(−2)(-3) + (-2) in 4 bits: 1100+1101=1 10011100 + 1101 = 1\,1001; end-around carry gives 1001+1=1010=−51001 + 1 = 1010 = -5.
  • 5+(−2)5 + (-2): 0101+1101=1 00100101 + 1101 = 1\,0010; end-around gives 0011=30011 = 3.
  • 3+(−3)3 + (-3): 0011+1100=11110011 + 1100 = 1111, no carry; the result is −0-0. Hardware must treat 11111111 as equal to 00000000.

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 0..2n−10..2^n-1.

Text
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 nn-bit word are all Θ(n)\Theta(n) 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 Z/2nZ\mathbb{Z}/2^n\mathbb{Z} with signed representatives in [−2n−1,2n−1−1][-2^{n-1}, 2^{n-1}-1]. Addition mod 2n2^n 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 −5-5 in 8 bits

EncodingPattern
Sign-magnitude1000010110000101
Ones' complement1111101011111010
Two's complement1111101111111011

Example 2: 4-bit two's complement table (selected)

PatternValuePatternValue
000001000−8-8
0001+1+11001−7-7
0111+7+71111−1-1

Example 3: 5+(−3)5 + (-3) and 5+65 + 6 in 4-bit two's complement

  • 0101+1101=00100101 + 1101 = 0010 (value 2); signs differ; no overflow.
  • 0101+0110=10110101 + 0110 = 1011 (value −5-5); both positive, result negative; overflow.

Example 4: negate 22 in 8 bits

22=0001011022 = 00010110. Invert 1110100111101001; add 1 →11101010\to 11101010. Decode: 234−256=−22234 - 256 = -22.

Example 5: encode −27-27 in 8 bits, all three ways

27=0001 101127 = 0001\,1011. Sign-magnitude sets the MSB: 1001 10111001\,1011. Ones' complement inverts every bit: 1110 01001110\,0100. Two's complement adds 1: 1110 01011110\,0101. Check with the weighted-sign view: −128+64+32+4+1=−27-128 + 64 + 32 + 4 + 1 = -27.

Example 6: the 8-bit pattern 1000 00001000\,0000

Unsigned 128; sign-magnitude −0-0; ones' complement −127-127; two's complement −128-128. Negating it in two's complement returns the same pattern (0111 1111+1=1000 00000111\,1111 + 1 = 1000\,0000) with V=1V = 1.

Example 7: sign extension and arithmetic shift

  • −5-5 in 4 bits is 10111011; sign-extended to 8 bits it is 1111 10111111\,1011, still −5-5. Zero-extension would give 0000 1011=+110000\,1011 = +11.
  • Why sign extension is exact. Adding a new top bit of weight −2n-2^{n} and turning the old sign bit's weight from −2n−1-2^{n-1} into +2n−1+2^{n-1} changes the value by −2n+2n−1+2n−1=0-2^n + 2^{n-1} + 2^{n-1} = 0.
  • Arithmetic right shift divides by 2 and rounds toward −∞-\infty: 1011≫1=1101=−31011 \gg 1 = 1101 = -3, which is ⌊−5/2⌋\lfloor -5/2 \rfloor, not the truncated −2-2 that integer division in C gives.

Important properties and observations

  • −1-1 in nn-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 nn-bit two's-complement value to m>nm > n 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 −(2n−1−1)-(2^{n-1}-1); two's complement has one zero and reaches −2n−1-2^{n-1}.
  • "A carry out means the signed result is wrong." 5+(−3)5 + (-3) produces a carry out and a correct signed result; 5+35 + 3 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 nn bits, two's minimum is −2n−1-2^{n-1}, maximum 2n−1−12^{n-1}-1.
  • NEGATION EDGE. Negating 100…0100\ldots 0 in two's complement has no representable result.
  • WHICH ZERO. Count distinct zeros under each encoding (2, 2, 1).
  • FLAG TABLES. Give both CC and VV for an addition; they are independent, and the carry-in XOR carry-out rule gives VV.
  • 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 −∞-\infty.

Summary

Sources

  1. 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).

Next steps