Skip to main content

Twos Complement Calculator

Free Twos complement Calculator for number systems. Enter values to get step-by-step solutions with formulas and graphs.

Reviewed by Manoj Kumar, Mathematics Educator

Reviewed by Manoj Kumar, Mathematics Educator

Formula

Negative: Invert bits + 1 | Range: -2^(n-1) to 2^(n-1)-1

To get the two's complement of a negative number, write the absolute value in binary, invert all bits, then add 1. For n bits, the representable range is from -2^(n-1) to 2^(n-1) - 1.

Worked Examples

Example 1: Converting -42 to 8-bit Two's Complement

Problem:Represent the decimal number -42 in 8-bit two's complement binary.

Solution:Step 1: Convert |42| to binary: 00101010\nStep 2: Invert all bits (ones' complement): 11010101\nStep 3: Add 1: 11010101 + 1 = 11010110\nVerification: 11010110 unsigned = 214, and 214 - 256 = -42

Result:Two's complement of -42 = 11010110 (0xD6) | Unsigned value: 214

Example 2: Two's Complement Addition

Problem:Compute 25 + (-17) using 8-bit two's complement arithmetic.

Solution:25 in binary: 00011001\n-17: |17| = 00010001, invert = 11101110, add 1 = 11101111\nAdd: 00011001 + 11101111 = 100001000\nDiscard carry bit (9th bit): 00001000 = 8\n25 + (-17) = 8 (correct!)

Result:25 + (-17) = 00001000 = 8 in decimal | Carry discarded

Frequently Asked Questions

What is two's complement and why is it used in computing?

Two's complement is the most widely used method for representing signed integers in binary. It assigns the most significant bit as the sign bit (0 for positive, 1 for negative) and represents negative numbers by inverting all bits of the absolute value and adding 1. The key advantage of two's complement is that addition and subtraction use the same hardware circuit regardless of sign. Unlike sign-magnitude or ones' complement, there is only one representation of zero. Two's complement also preserves the natural binary counting order for positive numbers and allows straightforward comparison operations. Virtually all modern processors, from microcontrollers to supercomputers, use two's complement for integer arithmetic.

How do you convert a negative number to two's complement?

Converting a negative decimal number to two's complement involves three steps. First, write the absolute value in binary using the specified number of bits. Second, invert (flip) every bit: change all 0s to 1s and all 1s to 0s. This gives the ones' complement. Third, add 1 to the result to get the two's complement. For example, to represent -42 in 8-bit two's complement: |42| in binary is 00101010. Inverting gives 11010101 (ones' complement). Adding 1 gives 11010110 (two's complement). You can verify by adding: 00101010 + 11010110 = 100000000 (the carry-out is discarded in 8-bit arithmetic, giving 00000000 = 0).

How does two's complement arithmetic work?

The beauty of two's complement is that ordinary binary addition works for both positive and negative numbers without special handling. To subtract A - B, you compute A + (-B) where -B is the two's complement of B. For example, 5 - 3 in 8-bit: 5 = 00000101, -3 = 11111101. Adding: 00000101 + 11111101 = 100000010. Discarding the carry gives 00000010 = 2, which is correct. Multiplication also works directly, though the result needs proper sign extension. Division requires additional logic for handling signs. Overflow occurs when the result exceeds the representable range, detected when two positive numbers sum to a negative or two negatives sum to a positive.

What is the difference between two's complement, ones' complement, and sign-magnitude?

These three systems represent signed integers differently. Sign-magnitude uses the first bit as a sign flag and remaining bits as the magnitude, creating two representations of zero (+0 and -0). Ones' complement represents negatives by inverting all bits, also producing two zeros. Two's complement inverts and adds 1, yielding a unique zero and allowing the simplest arithmetic circuits. Sign-magnitude is intuitive but requires separate addition and subtraction hardware. Ones' complement was used in early computers like the CDC 6600 but adds complexity due to end-around carry. Two's complement won out because it simplifies hardware design, eliminates the double-zero problem, and makes comparison straightforward.

References

Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy