Compute the two’s complement of a value or pattern: invert every bit, then add one. This is how virtually all modern CPUs represent negative integers.
Show calculation steps
Processed privately in your browser — nothing you paste is uploaded, logged or stored.
Negation, the hardware way
The two’s complement of an n-bit pattern is 2ⁿ minus its value — computed mechanically as invert-all-bits-then-add-one. It is simultaneously how negatives are ENCODED and how negation is PERFORMED: complementing the pattern for +42 yields the pattern for −42, and vice versa.
This calculator accepts either a signed decimal (to find its stored pattern) or a raw pattern (to compute its complement), at 4 to 64 bits, with the two-step pipeline traced.
Worked example
Pattern: 11010110 (holds −42 at 8 bits) invert 00101001 +1 00101010 = +42 ✓ The famous edge: 10000000 (−128) invert 01111111 +1 10000000 — its own complement; +128 does not fit
The one-pass shortcut
From the right, copy up to and including the first 1, then invert everything left of it: 11010110 → keep “10”, invert “110101” → 00101010. One scan, no addition — the trick professionals actually use, and worth verifying here until it is reflex.
Why subtraction hardware cares
ALUs subtract by adding the complement: a − b = a + (~b + 1), reusing the adder with a carry-in of 1. Every subtraction your CPU has ever performed took this path — the calculator’s trace is a window into it.
Privacy
All complements are computed locally; nothing you enter leaves the page.
How to use the Two's Complement Calculator
- Enter either a signed decimal (to find its representation) or a bit pattern (to compute its complement) – set which with the input option.
- Choose the bit width.
- Click "Compute complement".
- The steps show invert-then-add-one applied to your input, with the interpretation line explaining what negated what.
Frequently asked questions
What exactly does "taking the two's complement" do?
It negates: the two's complement of a pattern encodes the arithmetic negative of whatever the pattern encoded. Complementing 11010110 (−42) yields 00101010 (+42) – and complementing that returns the original.
Why does invert-plus-one equal negation?
Inverting gives (2ⁿ−1) − x; adding one makes it 2ⁿ − x, which is precisely how −x is stored in modulo-2ⁿ arithmetic. Subtraction hardware exploits this: a − b is computed as a + twos(b).
What happens at the minimum value, −128 in 8 bits?
Its complement is itself: invert 10000000 → 01111111, add one → 10000000 again. Arithmetically negating −128 overflows – the calculator shows the wraparound honestly, the same behaviour a CPU exhibits.
Is there a shortcut to compute it by hand?
Yes: scan from the right, keep everything up to and including the first 1, invert all bits left of it. 11010110 → keep "10", invert the rest → 00101010. One pass, no addition.
What is the difference from the one's complement?
Exactly the "+1". One's complement is inversion alone, an older scheme with two zeros; two's complement shifts the negatives by one, removing −0 and gaining one extra negative value.
Which bit width should I pick?
The width of the register or field the value lives in – the complement of the SAME decimal differs at each width because the leading sign-extension bits invert too. 8, 16, 32 and 64 cover almost every real case.
Does this run in my browser?
Fully – patterns and values are processed locally, never uploaded, never logged.