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 along. - 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.