Twos complement calculator

This twos complement calculator converts signed integers to binary and back. Example: −42 is 1101 0110 in 8 bits.

Source: Hennessy/Patterson, Computer Organization and Design, Kap. „Signed and Unsigned Numbers“ (Zweierkomplement). Updated: .

Whole number with sign. Thousands separators are allowed (1,024).
My ToolboxYour inputs are saved in this browser only.

Result

Result
-42 = 1101 0110 (two’s complement, 8 bits)
All representations
QuantityValue
Decimal value-42
Two’s complement1101 0110
Ones’ complement representation1101 0101
Hexadecimal0xD6
Range-128 … 127
Magnitude in binary0010 1010
Calculation steps
  • Magnitude |-42| in binary (8 bits): 0010 1010.
  • Flip all bits (ones’ complement): 1101 0101.
  • Add 1 (two’s complement): 1101 0110.
Two’s complement is the usual way computers store signed integers. It has a single zero and a range of −2^(n−1) to 2^(n−1) − 1.

How it is calculated

Two’s complement is the standard way computers represent negative integers. The highest bit acts as the sign, and addition and subtraction work with the same circuitry – no special cases needed.

How the conversion works

A non-negative number is written in binary as usual and padded with zeros on the left. For a negative number: write the magnitude in binary, flip all bits (ones’ complement) and add 1. In reverse: if the highest bit is set, the value is u − 2ⁿ, where u is the bit string read as an unsigned number. With n bits you can represent −2^(n−1) to 2^(n−1) − 1, so for 8 bits -128 to 127.

Worked example

−42 in 8 bits: the magnitude 42 is 0010 1010. Flipping gives 1101 0101 (ones’ complement), adding 1 gives 1101 0110, in hexadecimal D6. Check: 1101 0110 read as an unsigned number is 214, and 214 − 256 = -42. The bit string 1000 0000 stands for -128, the smallest 8-bit number; +128 no longer fits. In 16 bits, −1 is FFFF in hexadecimal. Read as ones’ complement, 1101 0110 would have the value -41.

Frequently asked questions

How do you convert a negative number to two’s complement?

Write the magnitude in binary, invert all bits and add 1. For −42 in 8 bits: 0010 1010 → 1101 0101 → 1101 0110.

What is the difference between ones’ and two’s complement?

Ones’ complement only flips the bits; two’s complement adds 1 afterwards. That leaves a single zero and lets arithmetic work without special rules.

What range does a byte have in two’s complement?

With 8 bits from -128 to 127, which is 256 different values.

Sources and legal basis

As of: · Editorial policy

Related tools