Two's Complement Calculator
Convert between signed and unsigned binary representations. Negate any integer in two's complement form, shown in binary, hex, and decimal across 8/16/32/64-bit widths.
How to Use
- Enter a whole number in decimal, positive or negative.
- Pick the width: 8, 16, 32 or 64 bits.
- Read the signed and unsigned values, the hex pattern and whether the number fits; “Unsigned only” means it fits as unsigned but not as signed.
- Check Show Work for the bit pattern and, for negative numbers, the flip-the-bits-and-add-one steps.
Worked Example
Storing −42 in 8 bits. 42 in binary is 00101010. Flip every bit to get 11010101, then add 1: 11010110, or 0xD6. Read as an unsigned number the same pattern is 214, which is 256 − 42.
Widening it: in 16 bits −42 is 0xFFD6. Sign extension copies the sign bit into the new upper bits, so the value is still −42.
The common mistake: reading signed data as unsigned. A signed 16-bit sensor value of 0xFFD6 is −42, but code that treats it as unsigned reports 65,494. Check the datasheet for whether each register is signed.
Show Work
Formulas
History of Two's Complement
There are three common ways to store negative numbers in binary: sign-magnitude (one bit for the sign, the rest for the size), one's complement (flip all the bits to negate) and two's complement (flip all the bits and add 1). Early computers used all three: the IBM 7090 used sign-magnitude, DEC's PDP-1 one's complement, and IBM's System/360, announced in 1964, two's complement, as almost every later design has.
Two's complement won for practical reasons: it has a single zero, addition and subtraction use the same circuit as unsigned numbers, widening a number (sign extension) only copies the top bit, and signed overflow is detected by comparing the carries into and out of the sign bit.
Today x86, ARM, RISC-V and the other mainstream architectures all use two's complement. C++20 and C23 made it the only representation their standards allow for signed integers; earlier versions also permitted one's complement and sign-magnitude. The uneven range is the one trap: the most negative value has no positive counterpart, so negating it overflows.
About This Calculator
Enter any decimal integer and pick a width of 8, 16, 32 or 64 bits. The tool shows the bit pattern, the hex value, the signed and unsigned readings of the same bits, and whether the number fits the signed range, fits only as unsigned, or had to wrap. The arithmetic uses exact big integers, so 64-bit values are not rounded.
Useful for: interpreting CPU debugger output, validating firmware that reads signed sensor values from memory-mapped registers, understanding why 127+1 wraps to −128 in 8-bit signed arithmetic, and converting between signed and unsigned interpretations of the same bit pattern. Everything runs in your browser.
Frequently Asked Questions
What is two's complement?
The standard way computers store signed integers. Positive numbers are plain binary; a negative number −x is stored as 2^N − x, so the top bit works as a sign bit. In 8 bits, −42 is stored as 256 − 42 = 214, the pattern 11010110.
How do I negate a number by hand?
Flip every bit and add 1. 42 is 00101010; flipped it is 11010101; adding 1 gives 11010110, which is −42. Doing the same again gets back to 42.
What range can N bits hold?
From −2^(N−1) to 2^(N−1) − 1: −128 to 127 in 8 bits, −32,768 to 32,767 in 16 bits and −2,147,483,648 to 2,147,483,647 in 32 bits. There is one more negative value than positive, because zero takes one of the non-negative patterns.
Why not sign-magnitude?
Sign-magnitude has two zeros (+0 and −0) and needs separate logic for adding numbers of different signs; 8 bits then hold only −127 to 127. With two's complement the same adder handles signed and unsigned numbers, and widening a number just copies the sign bit.
What happens on overflow?
The result wraps modulo 2^N: in 8 bits, 127 + 1 gives 10000000, which is −128. Processors flag signed overflow when the carry into the sign bit differs from the carry out of it. In C and C++, signed overflow is undefined behaviour, so do not rely on the wrap.
How do I use the Two's Complement Calculator?
Simply type your numbers and read the result, which refreshes the instant you change something. There is nothing to submit and nothing to wait for.
Does it cost anything or need an account?
No. The tool is completely free, there is no account to create, and it keeps working offline after the page first loads.
Is anything I type uploaded?
No. The tool works entirely on your device, so the values you enter never leave your browser.
Common Use Cases
Reading Sensor Registers
A signed 16-bit register that reads 0xFFD6 holds −42, not 65,494; reading it as unsigned is a common driver bug.
Raw Bytes from a Bus
A byte of 200 (0xC8) from a serial link means −56 if the protocol says the field is signed.
Debugging Overflow
An 8-bit counter or int8 variable that goes from 127 to −128 has overflowed; the bit pattern 0x7F + 1 = 0x80 shows why.
Memory Dumps
−1 in 64 bits is 0xFFFFFFFFFFFFFFFF, which a debugger may show as 18,446,744,073,709,551,615 when it treats the value as unsigned.
Last updated: