Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 57 ยท Digital Foundations

Signed Numbers and Two's Complement

Learn sign-magnitude, ones' complement and two's complement, number ranges, overflow detection and sign extension, with live Python tables you can run.

The binary numbers in the previous lesson were all unsigned: zero or larger. Real programs need negative numbers too, and hardware has no minus sign, only bits. Somehow a pattern of zeros and ones has to say "this value is below zero", and the circuit that adds numbers should work without caring whether they are positive or negative.

Three schemes have been used: sign-magnitude, ones' complement and two's complement. Two's complement won, and it is what practically every processor, language and file format uses today. This lesson shows all three, so you understand why the winner won, and then covers range, overflow and sign extension.

Fixed width matters

All signed formats assume a fixed number of bits, for example 4 or 8. With n bits there are exactly 2โฟ patterns. A signed scheme decides which of those patterns stand for negative numbers. Throughout this lesson we use 4 bits so every pattern fits in a table, but the rules are identical for 8, 16, 32 or 64 bits.

Sign-magnitude

The most obvious idea: use the leftmost bit (the MSB) as a sign (0 for positive, 1 for negative) and the remaining bits as the magnitude.

With 4 bits, 0101 is +5 and 1101 is โˆ’5. It looks natural, but it has two problems:

  1. Two zeros. 0000 is +0 and 1000 is โˆ’0. Hardware must treat them as equal.
  2. Awkward arithmetic. Adding needs to look at the signs, and decide whether to add or subtract magnitudes and which sign to give the result.

Ones' complement

To negate a number in ones' complement, flip every bit. So +5 = 0101 becomes โˆ’5 = 1010. Positive numbers are the same as before, and the MSB still acts as the sign.

Addition becomes simpler (add everything, then add any final carry back in, called the end-around carry), but there are still two zeros: 0000 and 1111. That extra step and the double zero are why it faded away.

Two's complement

Two's complement negates by flipping every bit and then adding 1:

  • +5 = 0101
  • flip: 1010
  • add 1: 1011 = โˆ’5

Equivalently, the value of an n-bit two's complement pattern is the unsigned value, minus 2โฟ if the MSB is 1. For 1011: unsigned 11, minus 16 gives โˆ’5.

Another way to see it is as place values. The MSB is worth โˆ’2โฟโปยน instead of +2โฟโปยน. In 4 bits the weights are โˆ’8, +4, +2, +1, so 1011 = โˆ’8 + 2 + 1 = โˆ’5.

The benefits are decisive:

  • One zero. Only 0000 is zero.
  • The same adder for everything. Adding two's complement numbers with an ordinary binary adder gives the right answer whether the operands are positive or negative, ignoring the final carry out.
  • Subtraction is addition. a โˆ’ b is simply a + (โˆ’b), so a single adder circuit (with some inverters) does both. You will build it in a later lesson on subtractors.

Three ways to say minus

Sign-magnitude MSB is the sign, the rest is the size.
Ones' complement Negate by flipping all bits.
Two's complement Flip all bits, then add 1.

Only two's complement has one zero and a plain adder that works.

1 / 2

All 4-bit patterns side by side

Run the table below. It decodes every 4-bit pattern three ways, so you can see the two zeros of the older schemes and the extra negative value of two's complement.

Python Every 4-bit pattern, three interpretations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the output, 0000 and 1000 are both zero in sign-magnitude, 0000 and 1111 are both zero in ones' complement, and only two's complement lists eight negative values and eight non-negative ones: โˆ’8 to 7.

Range

For n bits:

Scheme Smallest Largest Zeros
Unsigned 0 2โฟ โˆ’ 1 1
Sign-magnitude โˆ’(2โฟโปยน โˆ’ 1) 2โฟโปยน โˆ’ 1 2
Ones' complement โˆ’(2โฟโปยน โˆ’ 1) 2โฟโปยน โˆ’ 1 2
Two's complement โˆ’2โฟโปยน 2โฟโปยน โˆ’ 1 1

An 8-bit two's complement number runs from โˆ’128 to 127; a 16-bit one from โˆ’32,768 to 32,767. The range is asymmetric: there is one more negative value than positive. The most negative number has no positive partner, and negating it gives itself back (1000 flipped is 0111, plus 1 is 1000 again).

Negating and adding in code

Python integers have unlimited size, so to imitate a fixed width we mask with & ((1 << n) - 1). The next example negates by "flip and add 1", decodes the results, and adds two numbers with a plain binary add followed by a mask: the same thing a hardware adder does.

Python Negate by flip and add 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sum of 5 and โˆ’12 comes out as 11111001, which is โˆ’7. No sign checks were needed; the adder does not know or care which patterns are "negative".

Overflow

The fixed width has a cost: results that do not fit wrap around. Adding two big positive 8-bit numbers can produce a pattern with the MSB set, which reads as a negative number. This is overflow.

Be careful to separate two ideas:

  • Carry out of the top bit matters for unsigned arithmetic.
  • Signed overflow is the condition for two's complement, and it has a simple rule: it happens when adding two numbers of the same sign gives a result of the opposite sign. Adding numbers of different signs can never overflow.

Hardware often computes it as "the carry into the MSB differs from the carry out of the MSB". The later adder lesson turns that rule into a flag.

Python Detecting signed overflow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the rows: 100 + 50 gives โˆ’106 with overflow flagged, because the real answer 150 does not fit in โˆ’128 to 127. The row โˆ’128 + โˆ’1 wraps to +127. And the case 100 + 20 stays in range, so there is no overflow. Notice that -100 + -50 produces a carry out of the top bit, but that carry is a normal part of the wraparound arithmetic and not an error by itself: the overflow flag, not the carry, answers the signed question.

Sign extension

To put a signed value into a wider register, you must preserve its value. For unsigned numbers you add zeros on the left. For two's complement you copy the sign bit into all the new positions, which is called sign extension.

  • 0101 (+5) becomes 0000 0101 (+5).
  • 1011 (โˆ’5) becomes 1111 1011 (โˆ’5).

The value is preserved because the old sign bit was worth โˆ’8, and in the wider number the four new 1s (weights โˆ’128, +64, +32, +16) add up to โˆ’16, which together with the +8 of the old bit gives โˆ’8 again. Zero-extending a negative number would turn โˆ’5 into +11, which is a classic bug when mixing signed and unsigned types in programs.

Python Sign extension versus zero extension

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the sign-extended column keeps the original value in every row; the zero-extended column changes negative numbers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the 8-bit two's complement representation of -1?

    Flip 00000001 to get 11111110, then add 1 to get 11111111. All ones always means -1 in two's complement.
  2. 2. What is the range of a 6-bit two's complement number?

    The range is -2^(n-1) to 2^(n-1) - 1, which for n = 6 is -32 to 31.
  3. 3. Which is a drawback of sign-magnitude and ones' complement that two's complement avoids?

    Both have a +0 and a -0 pattern, which complicates comparisons and arithmetic. Two's complement has a single zero.
  4. 4. When does signed overflow occur in addition?

    Same-sign operands with an opposite-sign result mean the true sum was outside the representable range. Mixed-sign operands cannot overflow.
  5. 5. Which 8-bit pattern is the sign extension of the 4-bit value 1001 (-7)?

    Copy the sign bit (1) into the four new upper positions. 11111001 is -7 in 8 bits.

Summary

  • Signed numbers use a fixed width; the MSB indicates the sign in all common schemes.
  • Sign-magnitude and ones' complement have two zeros and awkward arithmetic; two's complement has one zero and works with an ordinary binary adder.
  • To negate in two's complement, flip all bits and add 1; the MSB has weight โˆ’2โฟโปยน.
  • An n-bit two's complement number covers โˆ’2โฟโปยน to 2โฟโปยน โˆ’ 1; the most negative value has no positive twin.
  • Signed overflow means same-sign operands gave an opposite-sign result; sign extension copies the sign bit to widen a value safely.

Next: BCD, Gray Code and ASCII, other ways to encode information in bits.