Beetwise

How two’s complement works

Two’s complement stores a negative value in a fixed number of bits.

To get the negative form, invert every bit of the magnitude and then add 1.

In 8 bits, the register holds -6 as 11111010. The same bits give 250 for an unsigned value.

Try two’s complement in the bit editor

The bits of -6 in an 8-bit signed register. Edit the expression or select a bit to flip it.

7
6
5
4
3
2
1
0
-6
value · 8 bits · signed
hex
0xFA
Read the bits as
Bit width

Two’s complement step by step

Read the bits in the editor as a value: The steps follow the value above.

  1. Write the bits: 11111010
  2. The top bit is 1, so the value is negative.
  3. Invert every bit: 00000101
  4. Add 1: 00000110, which is 6.
  5. The signed value is -6.

The same 8 bits read both ways

The same 8 bits read both ways
BitsUnsignedSigned
0000000000
0000000111
01111111127127
10000000128-128
11111010250-6
11111111255-1

Where two’s complement is used

A processor uses one adder circuit for addition and for subtraction.

Two’s complement makes this possible, because a subtraction becomes an addition of the negative form.

This is the reason almost all hardware stores signed values in this form.

Two’s complement: points to note

  • The top bit is not a minus sign. The top bit is part of the value.
  • The range is not symmetric. An 8-bit signed value holds -128 to 127, because 0 uses one of the positive codes.
  • A change of the signed mode does not change the bits. The same bits then give a different value.
  • A change of the bit width does change the value. The value 300 in 8 bits becomes 44, because the register drops the higher bits.

The full bit editor

The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.

Open this value in the full editor

Questions about two’s complement

What is -1 in binary?
In 8 bits, -1 is 11111111. All bits have the value 1. The same bits give 255 for an unsigned value.
Why not use a sign bit and a magnitude?
A sign bit gives two codes for zero and needs a second circuit for subtraction. Two’s complement avoids both problems.
How do I convert back to a decimal value?
Invert every bit and add 1. Then read the result as a magnitude and put a minus sign in front of it.