Turn any integer into 8, 16, 32 or 64 bit sign-magnitude, ones' and two's complement, with bitwise operations and overflow detection
Sign-magnitude, ones' complement and two's complement are computed together in one table, no manual inverting and adding
8 bit for microcontrollers, 16 bit for legacy registers, 32 bit for int, 64 bit for long — switching recalculates instantly
The sign bit is highlighted on its own and the rest is split into groups of four, so the sign is impossible to miss
When an input or a result falls outside the width, the tool says so and explains that the shown pattern is truncated
Add, subtract, AND, OR and XOR two numbers, with both the bit patterns and the decimal result
Everything runs in your browser, nothing you type is uploaded, and it works offline
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.
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.
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.
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.
Computer architecture and digital logic assignments ask for all three notations of a number; compare against the table instead of redoing it by hand
A temperature field in a protocol is a signed 16 bit value: 0xFFF6 becomes -10 rather than 65526
Two large positives add up to a negative: verify that it is wraparound and not corrupted data
Writing a negative offset or threshold into a register means converting it to two's complement hex first
Why -1 is all ones, why n & -n isolates the lowest set bit — the patterns make it obvious
Projecting the three notations of -5 side by side beats inverting bit by bit on a whiteboard
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.
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.
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 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.
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.
| Decimal | Sign-magnitude | Ones' complement | Two's complement | Hex |
|---|---|---|---|---|
| 0 | 0000 0000 | 0000 0000 | 0000 0000 | 00 |
| 1 | 0000 0001 | 0000 0001 | 0000 0001 | 01 |
| 5 | 0000 0101 | 0000 0101 | 0000 0101 | 05 |
| 127 | 0111 1111 | 0111 1111 | 0111 1111 | 7F |
| -1 | 1000 0001 | 1111 1110 | 1111 1111 | FF |
| -5 | 1000 0101 | 1111 1010 | 1111 1011 | FB |
| -127 | 1111 1111 | 1000 0000 | 1000 0001 | 81 |
| -128 | not representable | not representable | 1000 0000 | 80 |