StatGardenREF. DESK
Calculators/Maths/Two's complement
Maths

Two's complement calculator

Converts a signed decimal integer to its binary two’s-complement form at a chosen bit width.

Published 9 August 2026 · Updated 24 September 2026

What this calculator does

Two's complement is how essentially every computer stores signed integers. Positive values are written in ordinary binary. A negative value is written by taking the binary of its magnitude, flipping every bit, and adding one. The top bit then serves as a sign indicator, set for negatives and clear for everything else.

The reason this representation won out over the alternatives is that addition and subtraction need no special handling at all. The same circuit that computes 5 + 3 computes 5 + (−3) correctly, with no test of sign anywhere in it. The price is a small asymmetry: an n-bit range runs from −2^(n−1) up to 2^(n−1) − 1, so there is always one more negative value available than positive.

The formula

FormulaFor a negative number: invert the bits of |n| in binary, then add 1. Range: −2^(bits−1) to 2^(bits−1)−1

A positive value is written directly in binary and padded with leading zeros out to the chosen width. A negative value is converted by adding it to 2 raised to the bit width, which is arithmetically identical to inverting the bits of its magnitude and adding one, and the result is the unsigned bit pattern actually held in memory. Values outside the representable range for the chosen width are rejected rather than silently wrapped around.

TermMeaning
Two's complementThe standard signed integer representation, in which a negative value is stored as 2 raised to the bit width, plus that value.
Bit widthHow many bits the value is stored in, which fixes the range that can be represented.
Sign bitThe topmost bit. It is set for every negative value, but it carries a weight of −2^(n−1) rather than acting as a plain minus sign.
Representable rangeThe span from −2^(n−1) to 2^(n−1) − 1 that a given bit width can hold.

The inputs explained

FieldWhat to enter
Decimal integerThe decimal integer to convert, positive or negative. It must fall inside the range the chosen bit width can represent.
Bit widthThe bit width, from 1 to 32. Common choices are 8, 16 and 32, matching the integer sizes most languages and hardware provide.

When to use it

Reading a register or memory dump

A hexadecimal or binary value taken from a debugger is ambiguous until its width and signedness are known. Converting a candidate decimal value at the same width confirms whether the pattern on screen is the negative number you expect.

Checking for overflow at a given width

The representable range shown here is the boundary a value has to stay inside. A figure that fits comfortably in 16 bits may not fit in 8, and this is the quickest way to confirm before a narrowing conversion.

Learning how signed binary works

Stepping through −1, 0 and 1 at a small bit width makes the pattern clear far faster than reading a description of it, particularly the way −1 is all ones rather than anything resembling a 1 with a sign attached.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

How are different values stored in 8 bits?

Six values spanning the whole of what 8 bits can hold, from the most negative to the most positive.

Across the full 8-bit range
Decimal valueBinary (8 bit)Unsigned value stored
-12810000000128
-1611110000240
-111111111255
0000000000
1000000011
12701111111127
Zero and the positive values are ordinary binary with a clear top bit. Every negative value has that top bit set: −1 is every bit set, and −128 is the top bit alone with nothing after it, the most negative value 8 bits can hold. There is no separate pattern for negative zero, which is exactly what frees up that extra slot at the bottom of the range.

How is −1 stored at different bit widths?

The value −1 converted at each of the common bit widths.

The same value, five widths
Bit widthBinary for −1Representable rangeUnsigned value stored
4 bit1111-8 to 715
8 bit11111111-128 to 127255
12 bit111111111111-2,048 to 2,0474,095
16 bit1111111111111111-32,768 to 32,76765,535
32 bit11111111111111111111111111111111-2,147,483,648 to 2,147,483,6474,294,967,295
−1 is all ones at every width, because adding 1 to it overflows to zero in exactly the number of bits available, which is what being −1 means here. The unsigned pattern it stores is therefore 2 raised to the bit width, minus 1: 15 at 4 bits, 255 at 8 bits and 4,294,967,295 at 32.

Questions

Why is the negative range one larger than the positive range?

Because there is only one pattern for zero. Representations that carry a separate sign bit end up with both a positive and a negative zero, wasting a pattern. Two's complement uses that pattern for an extra negative value instead, which is why 8 bits reach −128 but stop at 127.

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

Read it as an ordinary unsigned binary number first. If the top bit is clear, that is the answer. If the top bit is set, subtract 2 raised to the bit width from it. An 8-bit pattern reading 240 unsigned is 240 − 256, which is −16.

Is the top bit just a minus sign?

No, though it does indicate a negative value. It carries an actual place value of −2^(n−1), and the remaining bits add positive amounts to it. That is why 10000000 at 8 bits is −128 rather than −0, and it is what lets ordinary addition work unchanged on signed values.

What happens to a value outside the range?

This calculator rejects it and shows the range instead. Real hardware wraps around silently, so assigning 128 to a signed 8-bit variable typically yields −128 with no warning, which is where a good many overflow bugs come from.

To convert between decimal, binary, octal and hexadecimal generally, see the base converter. For AND, OR and XOR across two values, see the bitwise calculator.