Two's complement converter

Direction
Bit width
Range −128 to 127

Try
Two's complement
1101 0110
Signed
−42
Unsigned
214
Hex
0xD6

Bit pattern

The sign bit counts as a negative place value
  1. 1−27−128
  2. 12664
  3. 02532
  4. 12416
  5. 0238
  6. 1224
  7. 1212
  8. 0201

−128 + 64 + 16 + 4 + 2 = −42

Step by step

  1. Write 42 in binary, padded to 8 bits0010 1010
  2. Invert every bit1101 0101
  3. Add 11101 0110That's −42 in 8-bit two's complement.
Guide

How two's complement works

  1. Pick a width. n bits hold signed values from −2n−1 to 2n−1 − 1, so 8 bits hold −128 to 127.
  2. For zero or a positive number, write it in binary and pad it with zeros to n bits.
  3. For a negative number, write its size in n bits, invert every bit, then add 1.
  4. To read a pattern back, check the leftmost bit. If it's 1, invert and add 1 again to get the size, then make it negative.

Worked example

Storing −42 in 8 bits:

StepBits
Write 42 in 8 bits0010 1010
Invert every bit1101 0101
Add 11101 0110

Check it: the sign bit is worth −128, so −128 + 64 + 16 + 4 + 2 = −42.

−42 → 1101 0110

  • The leftmost bit is the sign bit: 0 for zero and positive numbers, 1 for negative ones.
  • There's one more negative value than positive. 8 bits reach −128 but only 127.
  • Adding and subtracting work the same way for signed and unsigned numbers, which is why computers use two's complement.

Signed ranges

two's complement
WidthSmallestLargest
8-bit−128127
16-bit−32,76832,767
32-bit−2,147,483,6482,147,483,647
64-bit−9,223,372,036,854,775,8089,223,372,036,854,775,807
Learn

Two's complement explained

Why computers store negative numbers this way, a faster shortcut, sign extension and overflow.

Inside a computer, negative numbers don't get a minus sign. A two's complement calculator shows how they're stored instead, as plain binary in a fixed number of bits. Enter a signed decimal number above to see its 8, 16, 32 or 64-bit two's complement pattern, or switch direction to decode a pattern back to decimal, with every step explained. This guide covers why two's complement exists, a faster way to find it by hand, and what happens when a number doesn't fit.

Why computers need a signed format

Plain binary only describes zero and positive numbers. To store negatives, a computer needs a rule for using some of its bit patterns as negative values. Early designs tried two simpler schemes. Sign-magnitude uses the leftmost bit as a minus sign, and ones' complement stores a negative number by inverting every bit of its positive form. Both work, but both have two versions of zero, for example 00000000 and 11111111 in 8-bit ones' complement, and both need extra circuitry to add numbers with different signs. Two's complement fixes both problems, which is why nearly every processor made today uses it.

How two's complement works

In an n-bit two's complement number, every bit has its usual place value except the leftmost one, which counts as negative. In 8 bits, that top bit is worth −128 instead of +128. So 11010110 is −128 + 64 + 16 + 4 + 2 = −42, and 01111111 is 127, the largest 8-bit value. If the top bit is 0, the number is zero or positive and reads like ordinary binary. If it's 1, the number is negative. That's why the top bit is often called the sign bit, even though it's really a negative place value.

Finding the two's complement of a negative number

The standard method has three steps: write the size of the number in binary, padded to the full width, invert every bit, then add 1. For −42 in 8 bits, 42 is 00101010, inverting gives 11010101, and adding 1 gives 11010110. There's also a shortcut that skips the addition. Starting from the right, copy every bit up to and including the first 1, then invert all the bits to its left. For 42, the rightmost bits 10 are copied and the remaining 001010 is inverted to 110101, giving 11010110 again. For 20, which is 00010100, you copy 100 and invert the rest to get 11101100, which is −20.

Why hardware loves two's complement

The big advantage of two's complement is that adding and subtracting work exactly the same way for signed and unsigned numbers. The processor uses one adder and simply ignores any carry out of the top bit. Adding −1 and 1 in 8 bits gives 11111111 + 00000001 = 1 00000000. Dropping the carry leaves 00000000, which is 0, exactly as it should be. Subtraction becomes addition too: to work out A − B, the processor adds A to the two's complement of B. You can try this yourself in the binary calculator with a fixed bit width.

Sign extension

When a signed value moves to a wider register, for example from 8 bits to 16, its sign bit is copied into the new bits on the left. This is called sign extension. −42 is 1101 0110 in 8 bits and 1111 1111 1101 0110 in 16 bits, and both mean −42. Positive numbers are extended with zeros instead. Switch between the widths above to watch the pattern grow while the value stays the same.

Overflow

Every width has a limited range. 8 bits hold −128 to 127, and 16 bits hold −32,768 to 32,767. When a result goes past the end of the range, it wraps around to the other end. Adding 1 to 127 in 8 bits gives 10000000, which is −128. This is called signed overflow, and it's a common source of bugs in programs that use fixed-size integers. This two's complement calculator tells you when a number is out of range for the chosen width, and the binary calculator flags overflow in its results.

Two's complement in programming languages

Most programming languages store signed integers as two's complement. Java's int is a 32-bit two's complement number, Rust's i8 to i128 types use it, and the C++20 and C23 standards require it. JavaScript numbers are floating point, but its bitwise operators work on 32-bit two's complement integers, which is why ~0 is −1 and -1 >>> 0 gives 4,294,967,295. In hex, −1 is 0xFF in 8 bits and 0xFFFFFFFF in 32 bits, a pattern you'll often see in debuggers.

Using the calculator well

  • Pick the width that matches your data type: 8 bits for a signed byte, 32 for a typical int, 64 for a long.
  • When decoding, shorter patterns are padded with zeros, so they read as positive. To read a short pattern at its own length, use the binary to decimal converter in two's complement mode.
  • Copy the plain bit pattern or the decimal value with the copy button.
  • For the mathematical binary form with a minus sign, use the decimal to binary converter.

The fastest mental check: if the top bit is 1, the number is negative, and inverting every bit then adding 1 gives you its size.

FAQ

Frequently asked questions

Quick answers about the two's complement calculator and how it works.

What is two's complement?

It's how computers store signed whole numbers. The leftmost bit has a negative place value, so in 8 bits it's worth −128 instead of 128. 11010110 is −128 + 64 + 16 + 4 + 2 = −42.

How do I find the two's complement of a negative number?

Write the number without its minus sign in binary, padded to the full width. Invert every bit, then add 1. For −42 in 8 bits: 00101010, then 11010101, then 11010110.

How do I convert two's complement back to decimal?

Check the leftmost bit. If it's 0, read the pattern as ordinary binary. If it's 1, invert every bit, add 1 and put a minus sign in front. Choose Binary to decimal above to see those steps.

What range of numbers fits in 8, 16, 32 and 64 bits?

8 bits: −128 to 127. 16 bits: −32,768 to 32,767. 32 bits: −2,147,483,648 to 2,147,483,647. 64 bits: −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Why is there one more negative number than positive?

Zero uses one of the patterns that start with 0, which leaves one fewer for positive numbers. The most negative value, 10000000 in 8 bits, has no positive partner: inverting it and adding 1 gives the same pattern back.

What if my bit pattern is shorter than the width?

It's padded with zeros on the left, so it reads as positive. 1010 in 8 bits is 00001010, or 10. To read a short pattern at its own length, use Binary to Decimal with Two's complement turned on.

How is two's complement different from ones' complement?

Ones' complement only inverts the bits, so −42 would be 11010101. It has two zeros, a positive and a negative one, which complicates arithmetic. Two's complement adds 1 to fix that, which is why nearly every computer uses it.

Still have a question?Contact us