Two's Complement Calculator

Input

Plain decimal is fine, and prefixed forms such as 0xFF or 0b1010 also work. Values outside the selected width are truncated and flagged as overflow.
—
—

Two's Complement Calculator Features

Three notations at once

Sign-magnitude, ones' complement and two's complement are computed together in one table, no manual inverting and adding

Four bit widths

8 bit for microcontrollers, 16 bit for legacy registers, 32 bit for int, 64 bit for long — switching recalculates instantly

Bit by bit, colour coded

The sign bit is highlighted on its own and the rest is split into groups of four, so the sign is impossible to miss

Overflow warnings

When an input or a result falls outside the width, the tool says so and explains that the shown pattern is truncated

Arithmetic and bitwise

Add, subtract, AND, OR and XOR two numbers, with both the bit patterns and the decimal result

Local, private computation

Everything runs in your browser, nothing you type is uploaded, and it works offline

How to Use the Two's Complement Calculator

1

Pick a mode and a width

Use "Single value" to see how one number is stored, or "Two operands" to check whether an addition overflows. Match the width to the real type: a C char is 8 bit, short is 16, int is usually 32 and long long is 64.

2

Type the integer

Decimal such as -5 or 100 is the easy path, but 0xFF and 0b1010 let you specify a pattern directly. Spaces and commas are ignored, which makes pasting long numbers painless.

3

Read the table

Top to bottom: hex, grouped binary, sign-magnitude, ones' complement, two's complement, unsigned value, signed value, representable range, bitwise NOT, shift left by one and arithmetic shift right by one. The grouped binary row is the quickest way to verify a pattern.

4

Verify with two operands

Switch to "Two operands", enter both numbers and choose an operator to see both input patterns, the result pattern and the overflow verdict — handy for checking homework or a register value.

Where the Two's Complement Calculator Helps

Checking coursework

Computer architecture and digital logic assignments ask for all three notations of a number; compare against the table instead of redoing it by hand

Decoding low level data

A temperature field in a protocol is a signed 16 bit value: 0xFFF6 becomes -10 rather than 65526

Confirming overflow behaviour

Two large positives add up to a negative: verify that it is wraparound and not corrupted data

Setting hardware registers

Writing a negative offset or threshold into a register means converting it to two's complement hex first

Interviews and practice

Why -1 is all ones, why n & -n isolates the lowest set bit — the patterns make it obvious

Teaching negative numbers

Projecting the three notations of -5 side by side beats inverting bit by bit on a whiteboard

Two's Complement in Depth

Why negative numbers use two's complement

Sign-magnitude was the intuitive first attempt, but it is awkward in practice: two zeros, a separate sign check for every addition, and a dedicated subtract circuit. Two's complement maps a negative number to the modulus minus its magnitude, so addition and subtraction collapse into a single operation and the sign bit takes part in the arithmetic like any other bit. The price is that a negative pattern no longer looks like its magnitude, which is why reading them by eye is unpleasant. Essentially every general purpose CPU uses two's complement for signed integers today, and Java, C# and Go all mandate it.

How the three notations relate

For positive numbers all three notations are identical; the differences appear only with negatives. Sign-magnitude is a one in the sign position followed by the magnitude, so -5 is 10000101. Ones' complement keeps the sign bit and inverts the rest, giving 11111010. Two's complement adds one to that, giving 11111011. The chain is simple: invert sign-magnitude to get ones' complement, add one to get two's complement. Ones' complement was used by some early machines and survives mainly as a way to explain where two's complement comes from.

The modular arithmetic behind it

An n bit two's complement number lives in the ring of integers modulo 2^n. With 8 bits the modulus is 256, and -5 is congruent to 251 because -5 plus 256 equals 251. So the pattern does not store a minus sign at all: it stores the congruent non-negative residue. This single idea explains a lot. Why 127 plus 1 becomes -128, because the sum wraps past the modulus. Why inverting and adding one yields the complement, because inverting n ones gives 2^n minus 1 minus the value. And why subtraction can be carried out as addition.

Wraparound is not the same as overflow

Wraparound is a hardware fact: the width is fixed, the high bits beyond it are dropped, and the result lands back inside the modulus. Overflow is a semantic judgement: the mathematical result no longer fits the signed range of that width. Both happen at the same time but they are different ideas — unsigned addition also wraps, yet nobody calls it overflow, it is just modular arithmetic. The classic way to detect signed overflow is to compare carries: if the carry into the sign bit differs from the carry out of it, overflow occurred. That is exactly how the OF flag is produced.

Shifts, inversions and everyday bit tricks

Two's complement gives a handful of useful identities. An arithmetic right shift by one equals division by two rounded down, so -5 shifts to -3 rather than -2. Inverting and adding one negates a value, which is why n & -n isolates the lowest set bit, the core move behind a Fenwick tree. x ^ -1 equals ~x, and x ^ x is always zero. Testing for oddness only needs the lowest bit, because its weight is 1 regardless of the sign. All of these follow from the definition of two's complement.

Quick Reference for 8 Bit Two's Complement

Decimal Sign-magnitude Ones' complement Two's complement Hex
00000 00000000 00000000 000000
10000 00010000 00010000 000101
50000 01010000 01010000 010105
1270111 11110111 11110111 11117F
-11000 00011111 11101111 1111FF
-51000 01011111 10101111 1011FB
-1271111 11111000 00001000 000181
-128not representablenot representable1000 000080
The 8 bit range is -128 to 127, 16 bit is -32768 to 32767, 32 bit is -2147483648 to 2147483647 and 64 bit is -9223372036854775808 to 9223372036854775807. Only -128 has no sign-magnitude or ones' complement form, because its magnitude does not fit in seven bits.

Frequently Asked Questions

Q: Why does two's complement turn subtraction into addition?
A: Because two's complement arithmetic is done modulo 2 to the power of n. With 8 bits the modulus is 256, so -5 is stored as 11111011, which reads as 251, and 251 is congruent to -5 mod 256. To compute 100 minus 5 you can instead compute 100 plus 251, giving 351, and 351 mod 256 is 95, the correct answer. That is why a CPU needs only one adder: subtraction becomes adding a negative number, and no separate subtract circuit is required.
Q: Why does -128 have no sign-magnitude or ones' complement form?
A: Sign-magnitude uses the top bit for the sign and the remaining seven bits for the magnitude, so the largest magnitude is 127 and the absolute value 128 simply does not fit. Ones' complement is built the same way: you need the magnitude first, then invert it. Two's complement never stores the magnitude separately, so 10000000 is a perfectly valid pattern for -128. That is why two's complement holds one more negative value than sign-magnitude.
Q: Why is the 8 bit range -128 to 127 instead of symmetric?
A: Eight bits give 256 distinct patterns. Two's complement assigns the 128 patterns with a zero top bit to 0 through 127, and the 128 patterns with a one top bit to -128 through -1. Zero occupies a single pattern, 00000000, with no separate negative zero, so the negative side gains one extra slot and that slot goes to -128. Sign-magnitude and ones' complement both waste a pattern on negative zero, which is why their range is only -127 to 127.
Q: What is 127 plus 1 in 8 bit two's complement?
A: It is -128. The pattern for 127 is 01111111, adding one gives 10000000, and the top bit flips from 0 to 1, which reads as -128. Mathematically that is wrong, but from the hardware's point of view it is exactly right: an 8 bit register has only eight slots, the carry out of the ninth bit is discarded, and the result wraps around inside the 256 patterns. A CPU detects signed overflow by comparing the carry into the sign bit with the carry out of it.
Q: What is the difference between arithmetic and logical right shift?
A: It comes down to what fills the vacated high bits. A logical right shift always fills with zeros and treats the value as a plain bit string, which suits unsigned numbers. An arithmetic right shift replicates the original sign bit, so a negative number keeps filling ones and the effect is division by two rounded down. The 8 bit pattern of -8 is 11111000; arithmetic shift right gives 11111100, which is -4, while logical shift right gives 01111100, which is 124. Left shifts have no such split.
Q: Is "invert and add one" the same as the definition of two's complement?
A: The result is the same but the viewpoint differs. "Invert every bit and add one" is a hand-calculation shortcut: write the magnitude in binary, flip all bits, add one, and you have the two's complement. The definition is the non-negative residue of the negative number modulo 2 to the power of n, that is 2^n minus the magnitude. The two agree because inverting n ones equals 2^n minus 1 minus the value, and adding one turns that into 2^n minus the value. The shortcut only applies to negatives; a positive number's two's complement is its own binary form.
Q: How far apart are the signed and unsigned readings of one pattern?
A: The difference is how the top bit is read. When the top bit is 0 the two readings are identical. When it is 1, the signed reading gives that bit a weight of minus 2 to the power of n-1 while the unsigned reading gives it plus 2 to the power of n-1, so the two values differ by 2^n. For the 8 bit pattern 11111011 the unsigned value is 251 and the signed value is 251 minus 256, that is -5. That is also why assigning an int to an unsigned variable yields a huge positive number.