Clever Quest Lab Tools

Two's Complement Calculator

2026-10-06

Convert numbers between signed decimal, two's complement binary and hexadecimal in 2 to 64 bits, see the invert-and-add-one steps, add and subtract binary numbers with carries and an overflow flag, and convert fractions such as 0.1 between binary, decimal and hex with the exact rounding error.

Bit width

The width applies to the converter and to the adder below. Changing it keeps the numbers you entered whenever they still fit.

Two's complement converter

Bits (click or tap a bit to flip it; the small number is the bit's weight)

Binary addition and subtraction

Operation

Base converter with fractions

Try:

Everything is calculated in your browser with exact integer arithmetic. Nothing you type is sent to a server.

How to use the two's complement calculator

  1. Choose a bit width with the quick buttons (4, 8, 16, 32 or 64) or type any custom width from 2 to 64.
  2. In the converter, type a signed decimal number, a binary pattern or a hexadecimal value. The other two fields follow as you type. For widths up to 16 the bit buttons flip single bits.
  3. Read the bit pattern with its sign bit, the hex value, the signed and unsigned readings and the range. For a negative number the converter lists the steps: binary of the magnitude, invert every bit, add 1.
  4. In the adder, choose whether the operands are binary, decimal or hex, type A and B, and pick add or subtract. The table shows the carries, the sum and the overflow flag.
  5. In the fraction converter, choose the input base, type a number such as 10.625 and set how many fractional bits to keep. Copy buttons copy a result without spaces.

How it works

In an n-bit two's complement number every bit has the usual weight of a power of two, except the leftmost bit, the sign bit, whose weight is negative:

value = −b(n−1) × 2^(n−1) + b(n−2) × 2^(n−2) + … + b(1) × 2 + b(0)

Here n is the bit width and b(i) is the bit at position i, either 0 or 1, counted from 0 at the right. For example 1111 1011 in 8 bits is −128 + 64 + 32 + 16 + 8 + 2 + 1 = −5.

To negate a number, invert every bit and add 1, discarding any carry beyond the top bit. This works because inverting a pattern b gives (2^n − 1) − b, and adding 1 gives 2^n − b. That has the same bits as −b, since arithmetic on n bits wraps around at 2^n.

An adder treats both patterns as plain unsigned binary numbers, adds them column by column and keeps the low n bits. Because −B has the same bits as 2^n − B, the same adder is correct for signed and unsigned numbers; only the reading of the answer differs. Subtraction needs no extra circuit: A − B is A + (NOT B) + 1, where the +1 enters as the carry into the lowest column.

For fractions the calculator multiplies the exact value by 2^k, where k is the number of fractional bits, rounds to the nearest whole number (ties go away from zero), and divides by 2^k again. The decimal digits shown for the stored value and for the error come from integer arithmetic, so no floating-point noise appears.

Examples

Example 1: −5 in 8 bits. The magnitude 5 is 0000 0101. Inverting gives 1111 1010, and adding 1 gives 1111 1011, which is hex FB. Read as an unsigned number the same bits are 251.

Example 2: 127 + 1 in 8 bits. Binary 0111 1111 + 0000 0001 = 1000 0000. The carry out of the top bit is 0, but a carry of 1 enters the sign bit, so signed overflow is YES: the answer reads −128 instead of 128.

Example 3: 5 − 7 in 8 bits. Enter decimal 5 and 7 and choose subtract. The adder computes 0000 0101 + 1111 1000 + 1 = 1111 1110, which is −2. No signed overflow occurs. The carry out is 0, which for subtraction means a borrow, because 5 is smaller than 7 as unsigned numbers.

Example 4: 0.1 with 8 fractional bits. 0.1 × 256 = 25.6, which rounds to 26. In binary that is 0.00011010. The stored value is 26/256 = 0.1015625 and the error is 0.0015625. With 16 fractional bits the result is 0.0001100110011010, stored value 0.100006103515625, error 0.000006103515625.

Signed overflow versus carry

Two different flags are easy to confuse. The carry out of the top bit matters for unsigned numbers: 255 + 1 in 8 bits is 1111 1111 + 0000 0001 = 0000 0000 with a carry out of 1. As unsigned numbers the result wrapped around. As signed numbers it is −1 + 1 = 0, which is correct, so signed overflow is NO.

Signed overflow happens when the true signed result does not fit in the range. The hardware rule is: overflow occurs when the carry into the sign bit differs from the carry out of it. It can only happen when two numbers of the same sign give a result of the opposite sign. Adding a positive and a negative number never overflows. For subtraction a carry out of 1 means no borrow, that is A is at least B as unsigned numbers.

Range, width and sign extension

An n-bit two's complement number covers −2^(n−1) to 2^(n−1) − 1. There is one more negative value than positive, and the most negative value has no positive partner: negating −128 in 8 bits gives 1000 0000 again.

BitsSmallest valueLargest value
4-87
8-128127
16-3276832767
32-21474836482147483647
64-92233720368547758089223372036854775807

To widen a number, copy the sign bit to the left. The 8-bit pattern 1111 1011 (−5) becomes 1111 1111 1111 1011 (hex FFFB) in 16 bits, still −5. Narrowing cuts bits off and changes the value unless the number fits.

Why 0.1 has no exact binary form

A binary fraction is a sum of halves, quarters, eighths and so on, so its denominator is a power of 2. The value 0.1 is 1/10, and 10 contains the factor 5, so no finite number of bits can match it. In binary 0.1 is 0.0001100110011…: after the first zero the block 0011 repeats forever, just as 1/3 repeats in decimal. Any fraction whose reduced denominator has a prime factor other than 2 behaves this way. It is also why many programming languages print 0.30000000000000004 for 0.1 + 0.2.

Frequently asked questions

Why do computers use two's complement?

It has a single zero and lets one adder handle signed and unsigned numbers. Subtraction becomes addition of the inverted number plus 1, so no separate subtractor is needed.

What if my binary input is shorter or longer than the width?

A shorter pattern is padded with zeros on the left. A longer one is rejected with a message, because cutting bits would silently change the value.

Why is 128 out of range in 8 bits?

With 8 bits the signed range is −128 to 127. The pattern 1000 0000 is −128, not 128, so 128 needs at least 9 bits.

What happens to the result when overflow occurs?

The adder keeps only the low n bits, so the answer wraps around. The tool shows those bits, their signed and unsigned readings, and the exact result so you can see what was lost.

How many fractional bits do I need?

A number that ends in a fraction with a power of 2 as denominator, such as 0.625, needs only a few bits and is shown as exact. Other fractions are rounded to the nearest multiple of 1/2^k, so each extra bit halves the largest possible error.

Is my data sent anywhere?

No. All calculations run in your browser and nothing is uploaded. Your last inputs are kept only in this browser's local storage.

Related tools