Almost every processor stores negative integers in two’s complement: the leftmost bit carries a negative weight, so the same adder circuit handles positive and negative numbers alike. This calculator shows how it works for 4-, 8-, 16-, 32- and 64-bit integers, using exact big-integer arithmetic.
It has three modes. Decimal → bits encodes a signed integer, showing the invert-and-add-one steps, the hex form, the ones’ complement for comparison and the value sign-extended to double width. Bits → decimal reads a binary or hex pattern back as a signed value. Add / subtract performs register arithmetic, shows the column addition, and flags signed overflow and the carry out separately — two things students often confuse.
For converting between arbitrary bases, including fractions, use the number base converter; for long binary arithmetic without a fixed width, the binary calculator.
How to use this calculator
- Choose the width: 8 bits for a byte, 16 for a short, 32 for an int, 64 for a long.
- Decimal → bits: type a whole number such as −5. Values outside the range are refused with the valid range.
- Bits → decimal: type a pattern such as 1111 1011 or 0xFB. Shorter patterns are padded with zeros.
- Add / subtract: type two integers within the range and pick + or −.
- Open the steps to follow each bit operation.
The formula
For an -bit word with bits , the signed value is:
The range is (−128 to 127 for 8 bits). A negative is stored as the unsigned number , which is the same as inverting the bits of and adding 1:
Addition is done modulo . Signed overflow happens when two operands of the same sign give a result of the opposite sign; the carry out of the top bit is simply discarded.
Worked example
−5 in 8 bits: 5 = 0000 0101 → invert → 1111 1010 → add 1 → 1111 1011 = FB in hex. Check: . Sign-extended to 16 bits it becomes 1111 1111 1111 1011.
127 + 1 in 8 bits: 0111 1111 + 0000 0001 = 1000 0000, read as −128: an overflow, since 128 does not fit. Likewise 100 + 50 gives −106, and −128 − 1 wraps to 127. And −1 in 16 bits is FFFF.
Two’s complement versus ones’ complement and sign-magnitude
| 8-bit system | −5 | Range | Zero |
|---|---|---|---|
| Sign-magnitude | 1000 0101 | −127 … 127 | two zeros (+0, −0) |
| Ones’ complement | 1111 1010 | −127 … 127 | two zeros |
| Two’s complement | 1111 1011 | −128 … 127 | one zero |
Two’s complement wins because it has a single zero, one extra negative value, and needs no special case in the adder: subtraction is just addition of the negated operand. Since C23 and C++20, the C and C++ standards require it for signed integers.
Sign extension
To widen a signed value, copy its sign bit into the new upper bits. −5 is 1111 1011 in 8 bits and 1111 1111 1111 1011 in 16 bits; +5 stays 0000 0101 and gains zeros. Padding a negative pattern with zeros instead would turn −5 into +251, a classic bug when mixing signed and unsigned types.
Frequently asked questions
How do I find the two’s complement of a number?
Write the absolute value in binary with the chosen number of bits, invert every bit, then add 1. For −5 in 8 bits: 0000 0101 → 1111 1010 → 1111 1011.
How do I convert two’s complement back to decimal?
If the leftmost bit is 0, read the number normally. If it is 1, invert the bits, add 1, read that value and put a minus sign in front — or subtract 2^n from the unsigned value.
What is the range of an 8-bit or 32-bit signed integer?
8 bits: −128 to 127. 16 bits: −32,768 to 32,767. 32 bits: −2,147,483,648 to 2,147,483,647. In general −2^(n−1) to 2^(n−1) − 1.
How do I detect overflow when adding?
Overflow occurs only when both operands have the same sign and the result has the other sign, for example 127 + 1 = −128 in 8 bits. Adding numbers of opposite signs never overflows.
Is the carry the same as overflow?
No. The carry out of the top bit matters for unsigned arithmetic. For signed numbers, −1 + 1 produces a carry but no overflow, while 127 + 1 overflows without a carry.
Sources
- Intel 64 and IA-32 Architectures Software Developer’s Manual, Vol. 1 §4.2.1 (signed integers)
- Cornell University CS, Two’s complement (T. Finley)
- Wikipedia, Two’s complement
- ISO/IEC JTC1 SC22 WG14, N2412: two’s complement sign representation for C2x (C23)
Last updated: