About

2.2 Signed Integers: Signed Magnitude & Complements

Theory: Signed Integers: Signed Magnitude & Complements

A computer stores numbers in fixed-size binary words (8, 16, 32, 64 bits). Unsigned binary only covers 0,1,2,…0, 1, 2, \dots The question for this section is: how do we encode negative numbers using only 0s and 1s? There is no “minus” symbol in hardware, so one of the bits has to carry the sign information. There are three main methods (Null & Lobur, Ch. 2):

  1. Signed magnitude
  2. One’s complement
  3. Two’s complement

Throughout, nn is the word size in bits and the leftmost bit (MSB) is the sign bit in all three schemes: 00 = positive, 11 = negative. Positive numbers look identical in all three; they differ only in how negatives are formed.

Signed Magnitude

Idea: copy what we do on paper. Use the leftmost bit for the sign and the remaining n−1n - 1 bits for the magnitude (absolute value) in ordinary binary.

 sign | magnitude (n-1 bits)
  0   | 0010010      = +18
  1   | 0010010      = -18

Range for nn bits: −(2n−1−1)-(2^{n-1} - 1) to +(2n−1−1)+(2^{n-1} - 1). For 8 bits: −127-127 to +127+127.

Problems:

Mini-example

+18=0 0010010+18 = 0\,0010010, −18=1 0010010-18 = 1\,0010010. Only the sign bit changes.

One’s Complement

Idea: represent −N-N by the complement of NN with respect to 2n−12^n - 1 (all ones):

−N  ⟶  (2n−1)−N-N \;\longrightarrow\; (2^n - 1) - N

Subtracting from all ones never borrows, so this is the same as flipping every bit (0↔10 \leftrightarrow 1). That is trivial to build in hardware (one NOT gate per bit).

For 4 bits: one’s complement of 01000100 is 1111−0100=10111111 - 0100 = 1011, which is just 01000100 with every bit flipped.

Range for nn bits: −(2n−1−1)-(2^{n-1} - 1) to +(2n−1−1)+(2^{n-1} - 1), same as signed magnitude.

Advantage: subtraction becomes addition, 5−2=5+(−2)5 - 2 = 5 + (-2), using the same adder (section 2.3).

Disadvantages: still two zeros (00⋯0=+000\cdots0 = +0 and 11⋯1=−011\cdots1 = -0), and addition needs an extra step (the end-around carry).

Mini-example

19=0001001119 = 00010011, so −19-19 in one’s complement is 1110110011101100 (every bit flipped).

Two’s Complement

Idea: represent −N-N by the complement with respect to 2n2^n:

−N  ⟶  2n−N=((2n−1)−N)+1-N \;\longrightarrow\; 2^n - N = \big((2^n - 1) - N\big) + 1

So: two’s complement = one’s complement + 1 (“flip all the bits, then add 1”).

Shortcut: scan from the right, copy every bit up to and including the first 1, then flip all the remaining bits to the left. Example: 44=0010 110044 = 0010\,1100. Copy the rightmost "100100", flip the rest "0010100101" → "1101011010": −44=1101 0100-44 = 1101\,0100.

Range for nn bits: −2n−1-2^{n-1} to +(2n−1−1)+(2^{n-1} - 1). For 4 bits: −8-8 to +7+7; for 8 bits: −128-128 to +127+127. One extra negative value, because there is only one zero.

Reading a two’s complement number: the MSB has weight −2n−1-2^{n-1} instead of +2n−1+2^{n-1}:

bn−1bn−2⋯b0=−bn−1×2n−1+∑i=0n−2bi×2ib_{n-1} b_{n-2} \cdots b_0 = -b_{n-1} \times 2^{n-1} + \sum_{i=0}^{n-2} b_i \times 2^i

Alternatively, if the sign bit is 1, take the two’s complement again to get the magnitude.

Advantages: one zero; addition and subtraction use a plain adder with no correction step (just discard the carry out of the MSB); larger range.

Mini-example

19=0001001119 = 00010011 → flip → 1110110011101100 → add 1 → −19=11101101-19 = 11101101. Check with weights: −128+64+32+8+4+1=−19-128 + 64 + 32 + 8 + 4 + 1 = -19 ✓.

Ranges for nn Bits

RepresentationSmallestLargest4 bits8 bits16 bitsZeros
Unsigned002n−12^n - 10 to 150 to 2550 to 65535one
Signed magnitude−(2n−1−1)-(2^{n-1}-1)2n−1−12^{n-1}-1−7 to +7−127 to +127−32767 to +32767two
One’s complement−(2n−1−1)-(2^{n-1}-1)2n−1−12^{n-1}-1−7 to +7−127 to +127−32767 to +32767two
Two’s complement−2n−1-2^{n-1}2n−1−12^{n-1}-1−8 to +7−128 to +127−32768 to +32767one

Comparing the Three on 3 Bits

PatternSigned magnitudeOne’s complementTwo’s complement
000+0+0+0
001+1+1+1
010+2+2+2
011+3+3+3
100−0−3−4
101−1−2−3
110−2−1−2
111−3−0−1

The three patterns with sign bit 0 agree; the four with sign bit 1 differ.

Sign Extension

To widen a number to more bits without changing its value:

RepresentationRule
Two’s complement / one’s complementCopy the sign bit into all the new high-order positions.
Signed magnitudeMove the sign bit to the new MSB and fill the new positions between the sign and the magnitude with 0s.
UnsignedFill with 0s (zero extension).

Mini-example

−3-3 in 4-bit two’s complement is 11011101. In 8 bits: 1111 11011111\,1101 (check: 256−3=253=111111012256 - 3 = 253 = 11111101_2 ✓). Padding with zeros instead (0000 1101=+130000\,1101 = +13) would be wrong.

Which Representation Is Best, and Why?

Judge them on three issues: (1) balance (as many negatives as positives?), (2) number of zeros, (3) ease of operations.

IssueSigned magnitudeOne’s complementTwo’s complement
Balancebalancedbalancedone extra negative
Zerostwo (+0, −0)two (+0, −0)one
Ease of operationsworst: sign logic, compare magnitudes, separate subtractoraddition only, but end-around carryaddition only, carry simply discarded

Two’s complement is the best and is what virtually every modern computer uses for integers: one unique zero (simpler zero tests), the widest range, and subtraction done by the same adder with no correction step. The slight imbalance (e.g. −128-128 has no positive partner in 8 bits) is a small price.


Worked Examples

Exercise 1

Represent +18+18, −18-18, +100+100 and −100-100 as 8-bit numbers in signed magnitude, one’s complement and two’s complement.

Show solution ↓
Solution

First write the magnitudes in 8-bit binary:

  • 18=16+2=0001 0010218 = 16 + 2 = 0001\,0010_2
  • 100=64+32+4=0110 01002100 = 64 + 32 + 4 = 0110\,0100_2

Positives are the same in all three systems. For the negatives:

  • Signed magnitude: set the sign bit to 1, keep the magnitude.
  • One’s complement: flip every bit of the positive pattern.
  • Two’s complement: one’s complement + 1.
ValueSigned magnitudeOne’s complementTwo’s complement
+18+180001 00100001 00100001 0010
−18-181001 00101110 11011110 1110
+100+1000110 01000110 01000110 0100
−100-1001110 01001001 10111001 1100

Checks (two’s complement −N=28−N-N = 2^8 - N): 256−18=238=1110 11102256 - 18 = 238 = 1110\,1110_2 ✓ and 256−100=156=1001 11002256 - 100 = 156 = 1001\,1100_2 ✓. One’s complement (255−N255 - N): 237=1110 11012237 = 1110\,1101_2 ✓ and 155=1001 10112155 = 1001\,1011_2 ✓.

Exercise 2

(a) Given 19=00010011219 = 00010011_2, find −19-19 in 8-bit one’s complement and two’s complement.

(b) Find −44-44 in 8-bit two’s complement using the copy-then-flip shortcut, and check it two other ways.

Show solution ↓
Solution

(a) One’s complement: flip every bit.

0001 0011  →  1110 1100(−19 in one’s complement)0001\,0011 \;\to\; 1110\,1100 \quad (-19 \text{ in one's complement})

Two’s complement: add 1 to the one’s complement.

1110 1100+1=1110 1101(−19 in two’s complement)1110\,1100 + 1 = 1110\,1101 \quad (-19 \text{ in two's complement})

Check: 1110 11012=237=256−191110\,1101_2 = 237 = 256 - 19 ✓.

(b) 44=32+8+4=0010 1100244 = 32 + 8 + 4 = 0010\,1100_2.

Scan from the right: the first 1 is at bit 2. Copy "100100", flip the remaining five bits "0010100101" → "1101011010":

−44=1101 0100-44 = 1101\,0100

Check 1 (flip + 1): 0010 1100→1101 0011→1101 0011+1=1101 01000010\,1100 \to 1101\,0011 \to 1101\,0011 + 1 = 1101\,0100 ✓.

Check 2 (weights): −128+64+16+4=−44-128 + 64 + 16 + 4 = -44 ✓.

Exercise 3

What decimal value does the 8-bit pattern 1011 01011011\,0101 represent if it is interpreted as (a) unsigned, (b) signed magnitude, (c) one’s complement, (d) two’s complement? Repeat for 0110 10010110\,1001.

Show solution ↓
Solution

Pattern 1011 01011011\,0101 (sign bit is 1):

(a) Unsigned: 128+32+16+4+1=181128 + 32 + 16 + 4 + 1 = 181.

(b) Signed magnitude: sign 1 (negative), magnitude 011 01012=32+16+4+1=53011\,0101_2 = 32 + 16 + 4 + 1 = 53. Value =−53= -53.

(c) One’s complement: negative, so flip to get the magnitude: 0100 10102=64+8+2=740100\,1010_2 = 64 + 8 + 2 = 74. Value =−74= -74. (Check: 181−255=−74181 - 255 = -74 ✓.)

(d) Two’s complement: negative, so flip and add 1: 0100 1010+1=0100 10112=750100\,1010 + 1 = 0100\,1011_2 = 75. Value =−75= -75. (Check with weights: −128+32+16+4+1=−75-128 + 32 + 16 + 4 + 1 = -75 ✓, or 181−256=−75181 - 256 = -75 ✓.)

Pattern 0110 10010110\,1001: the sign bit is 0, so it is positive and all four interpretations agree:

64+32+8+1=+105 in every representation.64 + 32 + 8 + 1 = +105 \text{ in every representation.}

For negative patterns, the three signed readings always differ: one’s complement is the two’s complement value +1+1 (here −74-74 vs −75-75), and signed magnitude is something else entirely.

Exercise 4

(a) Give the range of values for 4-bit, 8-bit and 16-bit two’s complement, and for 8-bit signed magnitude.

(b) What is the minimum number of bits needed to store −200-200 in two’s complement? In signed magnitude?

Show solution ↓
Solution

(a) Two’s complement range is −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1:

nnRange
4−23-2^3 to 23−12^3 - 1 = −8-8 to +7+7
8−27-2^7 to 27−12^7 - 1 = −128-128 to +127+127
16−215-2^{15} to 215−12^{15} - 1 = −32768-32768 to +32767+32767

8-bit signed magnitude: −(27−1)-(2^7 - 1) to 27−12^7 - 1 = −127-127 to +127+127 (one fewer negative value, because 1000 00001000\,0000 is −0-0).

(b) Two’s complement needs −2n−1≤−200-2^{n-1} \le -200, i.e. 2n−1≥2002^{n-1} \ge 200. Since 27=128<200≤256=282^7 = 128 \lt 200 \le 256 = 2^8, we need n−1=8n - 1 = 8, so n=9n = 9 bits. (−200=1 0011 10002-200 = 1\,0011\,1000_2: check −256+32+16+8=−200-256 + 32 + 16 + 8 = -200 ✓.)

Signed magnitude needs 2n−1−1≥2002^{n-1} - 1 \ge 200, so 2n−1≥2012^{n-1} \ge 201, again n=9n = 9 bits (sign bit + 8-bit magnitude 200=1100 10002200 = 1100\,1000_2, giving 1 1100 10001\,1100\,1000).

Exercise 5

Build the table of all 3-bit patterns and their values in signed magnitude, one’s complement and two’s complement. Then answer: how many distinct values can each scheme represent with 3 bits, and which patterns are “wasted”?

Show solution ↓
Solution

Positive patterns (sign bit 0) mean the same thing in all three. For sign bit 1:

  • Signed magnitude: 1xy=−(xy2)1xy = -(xy_2).
  • One’s complement: flip all bits to get the magnitude.
  • Two’s complement: weight of MSB is −4-4, so 1xy=−4+xy21xy = -4 + xy_2.
PatternSigned magnitudeOne’s complementTwo’s complement
000+0+0+0
001+1+1+1
010+2+2+2
011+3+3+3
100−0−3 (flip: 011)−4 (−4+0-4+0)
101−1−2 (flip: 010)−3 (−4+1-4+1)
110−2−1 (flip: 001)−2 (−4+2-4+2)
111−3−0 (flip: 000)−1 (−4+3-4+3)
  • Signed magnitude: 8 patterns but only 7 distinct values (−3-3 to +3+3); 100100 (−0-0) duplicates zero.
  • One’s complement: also 7 distinct values (−3-3 to +3+3); 111111 (−0-0) duplicates zero.
  • Two’s complement: all 8 patterns give distinct values (−4-4 to +3+3); no pattern is wasted.

This is exactly why two’s complement has the larger range.

Exercise 6

Sign-extend +5+5 and −5-5 from 4 bits to 8 bits in each of the three representations.

Show solution ↓
Solution

4-bit forms: +5=0101+5 = 0101 in all three. −5-5: signed magnitude 11011101, one’s complement 10101010, two’s complement 10111011.

ValueScheme4-bit8-bitRule used
+5+5all three01010000 0101sign bit 0 copied / zeros inserted
−5-5signed magnitude11011000 0101sign moves to MSB, zeros inserted after it
−5-5one’s complement10101111 1010copy sign bit 1
−5-5two’s complement10111111 1011copy sign bit 1

Checks: one’s complement 255−5=250=1111 10102255 - 5 = 250 = 1111\,1010_2 ✓; two’s complement 256−5=251=1111 10112256 - 5 = 251 = 1111\,1011_2 ✓; signed magnitude 1 000 01011\,000\,0101 = sign − , magnitude 5 ✓.

Exercise 7

Considering balance, number of zeros and ease of operations, which of the three representations is best? Justify your answer with a short example.

Show solution ↓
Solution

Two’s complement is best.

  1. Balance: signed magnitude and one’s complement are perfectly balanced (−127-127 to +127+127 in 8 bits); two’s complement has one extra negative (−128-128 to +127+127). This is a minor drawback.
  2. Number of zeros: signed magnitude and one’s complement each have two zeros (+0+0 and −0-0), so a test for zero must check two patterns and one pattern is wasted. Two’s complement has a single zero (0000 00000000\,0000).
  3. Ease of operations: in two’s complement, subtraction is just addition of the negated number, and any carry out of the MSB is discarded. Example (8 bits), 48+(−19)48 + (-19):
   0011 0000   (48)
 + 1110 1101   (-19)
 -----------
 1 0001 1101   -> discard carry -> 0001 1101 = 29

In one’s complement the same addition needs an extra end-around-carry addition; in signed magnitude the hardware must compare signs and magnitudes and subtract.

Hence two’s complement gives the simplest, fastest hardware and the largest range, which is why it is the standard for integers.