Electrical Engineering › Section R · Digital concepts, number systems, and codes › Topic R.5

Complements and signed numbers

A register holds only bits, so negative numbers need a convention. This topic covers the 1's and 2's complements, the three ways of writing signed integers, the floating-point format for very large and small values, and how 2's complement lets one adder both add and subtract, with a test for overflow.

The 1's complement

To form a 1's complement, invert every bit: each 0 becomes 1 and each 1 becomes 0. The 1's complement of 00101001 is 11010110. In hardware this is just a row of inverters, one per bit. Adding a number to its own 1's complement always gives all 1s, because every column holds one 0 and one 1:

00101001 + 11010110 = 11111111

Complements are the tool that lets digital systems represent negative numbers and turn subtraction into addition.

The 2's complement

The 2's complement is the 1's complement plus 1. For 00101001: invert to get 11010110, then add 1 to get 11010111. A shortcut avoids the addition: starting from the LSB, copy each bit up to and including the first 1, then invert every bit to its left. In 00101001 the first 1 is the LSB itself, so copy it and invert the rest, giving 11010111 again. In 00110100 the first 1 is in the third place: copy 100 and invert 00110 to 11001, giving 11001100. Taking the 2's complement twice returns the original number.

Three signed-number forms

In every signed form the leftmost bit is the sign bit: 0 for positive, 1 for negative. Positive numbers look the same in all three forms. In sign-magnitude form, the other bits hold the magnitude in plain binary. The 1's complement form writes a negative number as the bit-by-bit inverse of its positive version, and the 2's complement form uses that inverse plus 1. In 8 bits, \(+41 = 00101001\) in all three, while \(-41\) is 10101001 in sign-magnitude, 11010110 in 1's complement and 11010111 in 2's complement. Processors use 2's complement, because it has one zero, not two, and needs no special hardware to add.

Reading a 2's complement number

In an n-bit 2's complement number the sign bit has weight \(-2^{n-1}\) and every other bit its usual positive weight. For 8 bits the weights are \(-128, 64, 32, 16, 8, 4, 2, 1\). So

\begin{aligned} 11010111 &= -128 + 64 + 16 + 4 + 2 + 1 \\ &= -41 \\ 10110100 &= -128 + 32 + 16 + 4 = -76 \end{aligned}

An alternative is to take the 2's complement of a negative number to find its magnitude, then attach the minus sign. The range of n bits is \(-2^{n-1}\) to \(2^{n-1} - 1\): \(-128\) to \(+127\) for 8 bits and \(-32{,}768\) to \(+32{,}767\) for 16 bits.

Floating-point numbers

Integers cannot hold values such as \(6.02 \times 10^{23}\) or 0.00025, so processors also use floating point, binary scientific notation. In IEEE 754 single precision, a 32-bit word holds a sign bit S, an 8-bit exponent E and a 23-bit fraction F. A normalised value is \(1.F \times 2^{E - 127}\): the leading 1 is implied, not stored, and the exponent carries a bias of 127 so that it never needs a sign. For 6.5: \(6.5 = 110.1_2 = 1.101_2 \times 2^2\), so \(S = 0\), \(E = 2 + 127 = 129 = 10000001_2\) and F begins 101 followed by zeros, giving the word \(40D00000_{16}\).

Adding signed numbers

In 2's complement, signed numbers are added exactly like unsigned ones, sign bits included, and any carry out of the sign bit is discarded. Four cases arise: two positives, positive plus a smaller negative, positive plus a larger negative, and two negatives; one rule covers them all. Overflow happens when the true result lies outside the range, as when \(100 + 50 = 150\) is forced into 8 bits. It can only occur when both numbers have the same sign, and it shows up as a result whose sign bit differs from theirs:

01100100 + 00110010 = 10010110

which reads as \(-106\).

Worked example: adding with a discarded carry

Add \(+45\) and \(-18\) in 8-bit 2's complement. 1. Write \(+45 = 00101101\). 2. Find \(-18\): \(+18 = 00010010\), inverted 11101101, plus 1 gives 11101110. 3. Add, giving a 9-bit result:

00101101 + 11101110 = 1\,00011011

4. Discard the carry out of the sign bit: 00011011. 5. Read it: sign 0, value \(16 + 8 + 2 + 1 = 27\). Check: \(45 - 18 = 27\). No overflow is possible, because the two numbers have opposite signs.

Worked example: subtraction by addition

Compute \(20 - 35\) in 8-bit 2's complement. Subtracting a number is the same as adding its 2's complement, so an adder plus a row of inverters handles both operations. 1. \(+20 = 00010100\) and \(+35 = 00100011\). 2. Negate the subtrahend: invert 00100011 to 11011100, add 1 to get 11011101, which is \(-35\). 3. Add, with no carry out:

00010100 + 11011101 = 11110001

4. The sign bit is 1, so the result is negative: \(-128 + 64 + 32 + 16 + 1 = -15\). Check: \(20 - 35 = -15\).

Multiplying and dividing signed numbers

For multiplication and division, a simple method works on magnitudes and fixes the sign separately. 1. If a number is negative, take its 2's complement to get its magnitude. 2. Multiply by adding shifted partial products, or divide by repeated subtraction, as in "Binary numbers, conversion, and arithmetic". 3. The result is positive if the signs were the same and negative if they differed; for a negative result, take the 2's complement. So \(-6 \times 5\): magnitudes give \(6 \times 5 = 30 = 00011110\), the signs differ, and the answer is 11100010, which is \(-30\).

Common mistakes to avoid

First, forgetting to add 1: inverting the bits gives the 1's complement, not the 2's. Second, reading a negative 2's complement number as plain binary: 11111010 is \(-6\), not 250. Third, treating the carry out of the sign bit as overflow; in 2's complement it is simply discarded, and overflow is judged by the sign of the result. Fourth, giving 8-bit range as \(-127\) to \(+127\); that is the sign-magnitude and 1's complement range. 2's complement reaches \(-128\). Fifth, forgetting the bias of 127 when reading a floating-point exponent.

Key takeaways

The 1's complement inverts every bit; the 2's complement adds 1 to that, or copies bits up to the first 1 and inverts the rest. The sign bit is 0 for positive and 1 for negative in sign-magnitude, 1's complement and 2's complement forms; processors use 2's complement. Its MSB has weight −2ⁿ⁻¹, giving a range of −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. Floating point stores sign, biased exponent and fraction: 1.F × 2^(E − 127) in single precision. Signed numbers add directly with the end carry discarded; subtraction adds the 2's complement; overflow shows as a wrong sign when two like-signed numbers are added.

Flashcards

Flashcard

1's complement

Invert every bit

00101001 becomes 11010110.

Test your understanding

Question 1 of 5

What is the 8-bit 2's complement of 00110100?

Question 2 of 5

An 8-bit register holds 11111010 in 2's complement form. What decimal value does it represent?

Question 3 of 5

What range of integers can a 6-bit 2's complement number represent?

Question 4 of 5

Which of these sums overflows in 8-bit 2's complement arithmetic?

Question 5 of 5

In IEEE 754 single precision, what is stored in the exponent field for the value 1.25 × 2⁵?

← Previous topicNext topic →