Two's Complement Calculator
Enter a signed decimal integer, a binary number, or a hex value and this calculator converts it to two's complement binary. Choose your bit width (4, 8, 12, 16, 32, or 64 bits), switch between decimal, binary and hex input modes, and get the one's complement alongside the two's complement. The step-by-step panel shows exactly how the conversion is performed so you can follow the method by hand.
What is two's complement?
Two's complement is the standard method computers use to represent signed (positive and negative) integers in binary. Unlike earlier schemes, two's complement has a single representation of zero, and addition and subtraction work identically whether the numbers are positive or negative, which simplifies hardware design enormously. Virtually every modern CPU uses two's complement for integer arithmetic, from 8-bit microcontrollers to 64-bit server processors.
How to convert a negative number to two's complement
To encode a negative integer -N in two's complement at a given bit width: (1) write the magnitude N in binary and pad it to the chosen bit width; (2) flip every bit to get the one's complement; (3) add 1 to the result. For example, to encode -42 in 8 bits: 42 in binary is 00101010; flipping gives 11010101; adding 1 gives 11010110. You can verify by reading 11010110 back: the MSB is 1 so it is negative, flip all bits to get 00101001, add 1 to get 00101010, which is 42, so the value is -42.
How to read a two's complement number
Look at the most significant bit (the leftmost bit). If it is 0, the number is non-negative and you can read the binary value directly as a normal unsigned integer. If it is 1, the number is negative: flip all bits, add 1, read the resulting magnitude, then apply a minus sign. At 8-bit width the signed range is -128 to 127, at 16-bit it is -32,768 to 32,767, and at 32-bit it is -2,147,483,648 to 2,147,483,647.
One's complement vs. two's complement
One's complement simply flips every bit, but it has two representations of zero (00000000 and 11111111) and requires an extra end-around carry in addition circuits. Two's complement fixes both problems by adding 1 after flipping: zero has only one pattern, and the carry logic is identical for positive and negative numbers. Two's complement is universally preferred in modern hardware. This calculator shows both so you can see the relationship between them.
Bit width and overflow
The number of bits determines how many distinct values can be stored: n bits hold 2^n patterns. For signed two's complement, half those patterns are used for negative numbers. The most negative representable value at n bits is -2^(n-1), and the most positive is 2^(n-1) - 1. Adding 1 to the maximum positive value overflows back to the minimum negative value, a wrap-around effect called integer overflow that is a common source of bugs in software.
Two's complement ranges by bit width
| Bit width | Minimum (signed) | Maximum (signed) | Total patterns |
|---|---|---|---|
| 4 | -8 | 7 | 16 |
| 8 | -128 | 127 | 256 |
| 12 | -2,048 | 2,047 | 4,096 |
| 16 | -32,768 | 32,767 | 65,536 |
| 32 | -2,147,483,648 | 2,147,483,647 | 4,294,967,296 |
| 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,616 |
Signed (two's complement) minimum and maximum integers at common bit widths, and the total number of distinct patterns.
Frequently asked questions
Why does two's complement add 1 after flipping the bits?
Flipping all bits of a number N gives you -(N+1) in two's complement, which is one less than -N. Adding 1 corrects the off-by-one, giving exactly -N. This also ensures there is only one representation of zero: flip 00000000 to get 11111111 (one's complement of 0), then add 1 to get 100000000, but you drop the overflow bit, leaving 00000000 again.
What is the difference between signed and unsigned integers?
Unsigned integers treat the bit pattern as a non-negative number. All n bits encode values from 0 to 2^n - 1. Signed two's complement integers use the leading bit as a sign bit, so they cover -2^(n-1) to 2^(n-1) - 1. The bit patterns are identical; only the interpretation changes. For example, the 8-bit pattern 11111111 is 255 unsigned but -1 signed.
How does two's complement make subtraction easier for hardware?
Subtraction A - B is equivalent to A + (-B). Because negation in two's complement is just 'flip bits and add 1', the subtraction circuit can reuse the adder circuit with the second operand negated. The CPU does not need a separate subtract unit, which saves transistors and ensures consistent timing.
Why is the most negative value one larger in magnitude than the most positive?
At n bits there are 2^n total patterns. Zero takes one of the non-negative slots, leaving an odd number of values above and below zero. In two's complement, the non-negative range is 0 to 2^(n-1) - 1 (that is 2^(n-1) values including zero), and the negative range is -1 to -2^(n-1) (also 2^(n-1) values). So the negative range extends one further from zero than the positive range.
How do I convert a two's complement binary number to hexadecimal?
Group the bits from right to left into groups of four. Convert each group of four bits to a single hex digit (0-9, A-F). For example, the 8-bit two's complement 11010110 splits into 1101 and 0110, which are D and 6, giving 0xD6. This calculator does the conversion for you and also shows the reverse path from hex back to binary and decimal.
What happens when an integer overflows in two's complement?
Integer overflow wraps around. Adding 1 to the maximum positive value (for example 127 in 8-bit) produces the minimum negative value (-128 in 8-bit), because the bit pattern rolls over from 01111111 to 10000000. This wrap-around is defined and predictable in hardware, but in high-level languages like C it is undefined behaviour for signed integers, which is why it can cause subtle bugs.