Skip to content
All apps

Two's complement

About this app

Step by step: one's complement, +1, computing subtraction as addition — with freely chosen numbers and 4-/8-/16-bit display.

Negative numbers in binary

Computers store integers with a fixed number of bits. In two's complement you get −B by flipping every bit of B (the ones' complement) and then adding 1. The highest bit then indicates the sign. The big advantage: the subtraction A − B becomes the addition A + (−B), so the processor only needs an adder. A carry that falls out of the fixed width on the left is simply discarded. With 8 bits the range runs from −128 to 127.

What the app shows

The app works through A − B in separate steps. First the bit row of B, then the inverted row as the ones' complement, then the position where 1 is added, and finally the result row. In the last step it adds A + two's complement(B) and shows the result in decimal. A title and an explanation accompany every step.

Try it yourself

Pick A and B freely and switch between 4, 8 and 16 bits. Try a subtraction with a negative result and one where a carry falls out on the left. With 4 bits you quickly see where the range from −8 to 7 ends. Then compare the same calculation with 8 bits and notice how the leading ones of the negative number grow longer.

Frequently asked questions

How do you compute the two's complement of a number?
Write the number in binary with a fixed width, flip every bit (0 becomes 1 and vice versa) and add 1. Example with 8 bits: 5 = 00000101, flipped 11111010, plus 1 gives 11111011, which is −5.
Why do computers use two's complement?
Because addition and subtraction then use the same circuit: A − B is computed as A + (−B). There is also only one zero, and the highest bit directly shows the sign.
What range does n-bit two's complement cover?
From −2^(n−1) to 2^(n−1) − 1: with 4 bits −8 to 7, with 8 bits −128 to 127, with 16 bits −32768 to 32767. There is one more negative number than positive ones.
What can I do in the two's complement app?
You enter two numbers, choose 4, 8 or 16 bits and go through A − B step by step: flip the bits, add 1, add, and read the result in decimal.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps