About the Two's Complement Calculator
This two's complement calculator converts a signed decimal integer into the exact bit pattern a computer stores for it, and decodes any binary or hexadecimal pattern back into its signed and unsigned decimal values. Pick a width of 4, 8, 16, 32 or 64 bits — the same pattern means different numbers at different widths, so the width matters.
It is useful for students learning computer architecture, embedded and firmware developers reading register dumps, and anyone debugging why 0xFFFFFFFF shows up as −1 in one language and 4,294,967,295 in another. Alongside the pattern you get the one's complement, the allowed range for the width, and the unsigned interpretation of the same bits.
Binary and hex input is zero-extended to the chosen width: "1101" at 4 bits is −3, but at 8 bits it becomes 0000 1101 = 13. All arithmetic uses arbitrary-precision integers, so 64-bit values are exact.
How to use the two's complement calculator
- 1Choose whether you are starting from a decimal number, a binary pattern or a hex pattern.
- 2Type the value (negative decimals need a minus sign).
- 3Pick the bit width your language or hardware uses.
- 4Read the binary and hex pattern, or the signed and unsigned decimal value.
- 5Check the range line to confirm the number fits in that width.
Formula and method
In n-bit two's complement, non-negative numbers are stored as plain binary and a negative number x is stored as 2ⁿ + x — equivalently, write |x| in binary, invert every bit (the one's complement) and add 1. The most significant bit therefore acts as the sign bit with weight −2ⁿ⁻¹, giving a range of −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
Decoding reverses this: if the top bit is 0 the value is the unsigned number; if it is 1, subtract 2ⁿ. The same bits read as an unsigned integer are always the plain binary value, which is why 0xFF is 255 unsigned but −1 as a signed byte.
- n
- Bit width (4, 8, 16, 32 or 64)
- x
- Signed integer value
- pattern
- Stored bits read as an unsigned integer
Worked examples
−42 as an 8-bit signed byte
42 is 0010 1010. Inverting gives 1101 0101 and adding 1 gives 1101 0110 = 0xD6. Read as unsigned the same byte is 256 − 42 = 214.
−1,234 as a 16-bit short
2^16 − 1,234 = 64,302, which is 0xFB2E in hex. The leading 1 bit shows it is negative when interpreted as a signed 16-bit value.
Decode the 32-bit hex value FFFFFF80
0xFFFFFF80 is 4,294,967,168 unsigned. The top bit is set, so the signed value is 4,294,967,168 − 2^32 = −128 — the sign-extended form of the byte 0x80.
Decode a 64-bit pattern of all ones
Sixty-four 1 bits are 2^64 − 1 = 18,446,744,073,709,551,615 unsigned, but −1 in signed 64-bit arithmetic, the same way 0xFF is −1 as a signed byte.
Frequently asked questions
How do you find the two's complement of a number?+
Write the positive number in binary at the chosen width, invert every bit (0↔1) and add 1. For −5 in 8 bits: 5 = 0000 0101, inverted = 1111 1010, plus 1 = 1111 1011.
Why do computers use two's complement?+
It lets the same adder circuit handle signed and unsigned addition and subtraction, has only one representation of zero (unlike sign-magnitude or one's complement), and makes negation a simple invert-and-add-one.
What is the range of an 8-bit signed integer?+
From −128 to 127. In general an n-bit two's complement integer covers −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so 16-bit is −32,768 to 32,767 and 32-bit is −2,147,483,648 to 2,147,483,647.
How can I tell if a two’s complement number is negative?+
Look at the most significant (leftmost) bit at the full width. If it is 1 the number is negative; if it is 0 the number is zero or positive. That is why the bit width must be known before decoding.
What is the difference between one's complement and two's complement?+
One's complement simply inverts the bits, which leaves two zeros (+0 and −0). Two's complement adds 1 after inverting, removing the negative zero and giving one extra negative value, such as −128 for a byte.