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
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.
| Term | Meaning |
|---|---|
| Two's complement | The standard signed integer representation, in which a negative value is stored as 2 raised to the bit width, plus that value. |
| Bit width | How many bits the value is stored in, which fixes the range that can be represented. |
| Sign bit | The 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 range | The span from −2^(n−1) to 2^(n−1) − 1 that a given bit width can hold. |
The inputs explained
| Field | What to enter |
|---|---|
| Decimal integer | The decimal integer to convert, positive or negative. It must fall inside the range the chosen bit width can represent. |
| Bit width | The 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.
| Decimal value | Binary (8 bit) | Unsigned value stored |
|---|---|---|
| -128 | 10000000 | 128 |
| -16 | 11110000 | 240 |
| -1 | 11111111 | 255 |
| 0 | 00000000 | 0 |
| 1 | 00000001 | 1 |
| 127 | 01111111 | 127 |
How is −1 stored at different bit widths?
The value −1 converted at each of the common bit widths.
| Bit width | Binary for −1 | Representable range | Unsigned value stored |
|---|---|---|---|
| 4 bit | 1111 | -8 to 7 | 15 |
| 8 bit | 11111111 | -128 to 127 | 255 |
| 12 bit | 111111111111 | -2,048 to 2,047 | 4,095 |
| 16 bit | 1111111111111111 | -32,768 to 32,767 | 65,535 |
| 32 bit | 11111111111111111111111111111111 | -2,147,483,648 to 2,147,483,647 | 4,294,967,295 |
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.