CS With NehalCS With Nehal

Two's Complement Simulator

Convert between denary and two's complement binary, with the invert-and-add-one steps and the negative place value shown.

Denary → two's complement

With 8 bits you can store -128 to 127.

Bits
1. Write +45 in binary
0
0
1
0
1
1
0
1
2. One's complement (invert every bit)
1
1
0
1
0
0
1
0
3. Add 100000001
= -45 in two's complement
1
1
0
1
0
0
1
1
Shortcut: copy bits from the right up to and including the first 1, then invert the rest.

Two's complement → denary

Click bits to change them. The leftmost bit has a negative place value.

-128
64
32
16
8
4
2
1

-128 + 64 + 16 + 2 + 1 = -45

The sign bit is 1, so the number is negative.
-45
Signed value
211
Same bits as unsigned
-128
Smallest
10000000
127
Largest
01111111

Why two's complement?

  • There is only one zero (00000000), unlike sign and magnitude.
  • Subtraction becomes addition: A − B = A + (−B), so the CPU needs only an adder.
  • Negate any number by inverting the bits and adding 1.

About This Simulation

This two's complement simulator converts between denary and signed binary. It shows the invert-and-add-one method for negative numbers and lets you click bits to see how the negative place value of the leftmost bit gives the signed value.

Features

  • Any width from 1 to 16 bits, with its range
  • Denary to two's complement: binary, one's complement, then add 1
  • Clickable bits with place values, including the negative MSB
  • The same bits read as signed and unsigned
  • Smallest and largest values for each width

Topics covered

  • Two's Complement
  • Signed Binary
  • Sign Bit
  • Negative Numbers
  • Range of Values

Explanation

Two's complement is the standard way computers store negative integers. The leftmost bit (the most significant bit) has a negative place value, for example −128 in 8 bits, while the other bits are positive as usual. A leading 1 therefore means the number is negative.

How it works

  1. For a positive number, write it in normal binary with a leading 0.
  2. For a negative number, write the positive version in binary, invert every bit (0 ↔ 1), then add 1.
  3. Shortcut: copy the bits from the right up to and including the first 1, then invert all the remaining bits.
  4. To convert back, add up the place values of the 1 bits, remembering the leftmost one is negative.
  5. With n bits the range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so 8 bits store −128 to 127.

Why it matters

Two's complement is on the IGCSE and AS Level syllabuses. It lets the CPU subtract by adding a negative number, so the same adder circuit handles both operations, and it has only one representation of zero.

Frequently Asked Questions

How do you convert −45 to 8-bit two's complement?

+45 = 00101101. Invert: 11010010. Add 1: 11010011.

Why is two's complement used instead of sign and magnitude?

It has only one zero, and addition works the same for positive and negative numbers, so subtraction can be done by adding the two's complement.

What is the range of an 8-bit two's complement number?

−128 (10000000) to +127 (01111111).

How can you tell if a two's complement number is negative?

Its leftmost (most significant) bit is 1.

Related Simulations