How to use the two’s complement calculator
Choose a bit width, then type a decimal number such as −53 to see its two’s complement bits, or type a bit pattern such as 1100 1101 or 0xCD to see the number it stands for. The calculator gives:
- the signed and unsigned values, with hex and octal
- the one’s complement and the two’s complement (negation) of the bits
- the value sign-extended to twice the width, and the range at this width
- a comparison with sign-magnitude, one’s complement and offset binary
- the working step by step: invert and add 1, subtract from 2ⁿ, and the right-to-left shortcut
Click any bit to flip it. Under each bit is its place value, with −128 under the top bit of a byte, which is the whole idea of two’s complement in one picture.
The Signed arithmetic tab works with values already stored at the selected width:
- column addition with every carry shown
- subtraction done by adding the negated value
- multiplication with the full double-width product
- signed division and remainder, truncating the quotient toward zero
- the negative, zero, carry and overflow flags
Choose Match the digits typed to read a short pattern such as 1011 as a 4-bit number. Tick Wrap to see what happens when a value is forced into too few bits.
Use Convert result to another width to compare sign extension with zero extension when widening, or to keep the low bits when narrowing. The result states whether the original signed value survives the conversion.
What is two’s complement?
Two’s complement is the way virtually every computer stores signed whole numbers. In an n-bit number, the bits have their usual binary place values except the leftmost, which counts as negative. In 8 bits, the place values are −128, 64, 32, 16, 8, 4, 2 and 1. So 1100 1011 is −128 + 64 + 8 + 2 + 1 = −53, and 0111 1111 is 127.
This gives a range of −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so a byte holds −128 to 127. There is one more negative number than positive, because zero takes one of the non-negative patterns. Every negative number starts with a 1, so the top bit works as a sign bit. There is only one zero.
The main advantage is that addition and subtraction use exactly the same circuit for signed and unsigned numbers. The hardware just adds the bit patterns and throws away the final carry, and the answer comes out right as long as it fits.
How to find the two’s complement of a number
To write a negative number, or to negate any number, use one of three equivalent methods. Here is −90 in 8 bits:
- Invert and add 1. Write 90 in binary, 0101 1010. Flip every bit to get 1010 0101, the one’s complement. Add 1 to get 1010 0110.
- Subtract from 2ⁿ. 2⁸ − 90 = 166, and 166 in binary is 1010 0110.
- The shortcut. Start from the right of 0101 1010. Copy the bits up to and including the first 1 (“10”), then flip all the rest: 1010 0110.
Check: −128 + 32 + 4 + 2 = −90. Negating the result again gives back 0101 1010. The calculator shows all three methods for any number you enter.
Converting two’s complement to decimal
If the leftmost bit is 0, read the number as ordinary binary. If it is 1, the number is negative, and there are two ways to find its value. Take 1100 1101:
- Use the negative top bit: −128 + 64 + 8 + 4 + 1 = −51.
- Negate it: invert to 0011 0010, add 1 to get 0011 0011 = 51, so the original is −51.
The width matters. 1100 1101 is −51 as an 8-bit number but 205 as part of a 16-bit number, where it would be 0000 0000 1100 1101. A program or datasheet always tells you which width to use.
Ranges by bit width
| Bits | Common type names | Signed range (two’s complement) | Unsigned range |
|---|---|---|---|
| 4 | nibble | −8 to 7 | 0 to 15 |
| 8 | int8_t, sbyte, Java byte | −128 to 127 | 0 to 255 |
| 16 | int16_t, short | −32,768 to 32,767 | 0 to 65,535 |
| 32 | int32_t, int, Java int | −2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| 64 | int64_t, long, Java long | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
Signed arithmetic, division and overflow
To add two’s complement numbers, add the bits column by column like ordinary binary and ignore any carry out of the leftmost bit. To subtract, negate the second number and add: 45 − 72 becomes 0010 1101 + 1011 1000 = 1110 0101, which is −27.
Overflow happens when the true answer is outside the range. In 8 bits, 96 + 64 = 160 doesn’t fit, and the bits 1010 0000 read as −96. The test is simple. If two numbers with the same sign are added and the result has the opposite sign, the result has overflowed. Adding numbers with opposite signs can never overflow. A processor records this in its overflow flag (V on ARM, OF on x86).
Carry and overflow are different. The carry out of the top bit matters for unsigned numbers. Overflow matters for signed ones. Adding 1111 0110 and 0000 1101 produces a carry, but the signed result, −10 + 13 = 3, is correct. Multiplication needs twice the width for the full product. In 8 bits, 12 × −11 = −132 doesn’t fit, and keeping only the low byte gives 124.
Signed division truncates toward zero in languages such as C, C++, Java, C# and Rust. Therefore −17 ÷ 5 is −3, with remainder −2, because −17 = (−3 × 5) + (−2). Division by zero is invalid. Dividing the most negative value by −1 is also exceptional: its positive result is one above the maximum value at that width.
Sign extension and truncation
To widen a two’s complement number, copy its sign bit into all the new positions. As a byte, −53 is 1100 1011. As 16 bits it becomes 1111 1111 1100 1011, still −53. Padding with zeros instead would give 203, which is the classic bug when a signed byte is widened as if it were unsigned. Narrowing keeps only the low bits. That is correct when the value fits in the smaller width, and silently wrong otherwise.
Two’s complement vs one’s complement, sign-magnitude and offset binary
- Sign-magnitude uses the top bit as a plain sign and the rest as the size, like writing a minus sign. It has two zeros (+0 and −0) and needs separate circuits for adding and subtracting. Floating-point numbers use this idea for their sign bit.
- One’s complement negates by flipping every bit. It also has two zeros, and its additions need an “end-around carry”. It survives today in the Internet checksum used by IPv4, TCP and UDP.
- Offset binary (excess-K) adds a fixed bias, 128 for a byte, so that the smallest number is all zeros. It is used for floating-point exponents and in some analogue-to-digital converters.
- Two’s complement has one zero, one extra negative value and the simplest hardware, which is why it won. Since C++20 and C23, the C and C++ standards require it.
Where two’s complement matters
- Programming: every signed integer type in C, C++, Java, C#, Go and Rust. JavaScript’s bitwise operators also treat numbers as 32-bit two’s complement.
- Embedded systems and sensors: registers and data packets often hold signed readings. A 16-bit temperature value of 0xFF38 means −200, not 65,336, so −2.00 °C if the scale is hundredths of a degree.
- Assembly and computer architecture: the flags after an addition, signed versus unsigned comparisons, and arithmetic right shifts.
- Networking and file formats: decoding signed fields from hex dumps and binary protocols.
- Coursework: digital logic, computer organisation and exam questions on conversion and overflow.
Common mistakes
- Forgetting the width. “The two’s complement of 1011” depends on whether it is 4, 8 or 16 bits.
- Reading signed data as unsigned, or the other way round. The bits are the same; only the interpretation changes.
- Negating the most negative value. In 8 bits, −(−128) gives −128 again, and in C, abs(INT_MIN) overflows.
- Mixing signed and unsigned in C. In
-1 < 1u, the −1 is converted to 4,294,967,295, so the comparison is false. - Confusing carry with overflow when checking whether a signed result is valid.