Twos Complement Calculator
Free Twos complement Calculator for number systems. Enter values to get step-by-step solutions with formulas and graphs.
Reviewed for accuracy by Manoj Kumar, Mathematics Educator
Twos Complement Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser โ no data is sent to any server.
Formula: Negative: Invert bits + 1 | Range: -2^(n-1) to 2^(n-1)-1
Worked example โ Two's complement of -42 = 11010110 (0xD6) | Unsigned value: 214
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 Step 2: Invert all bits (ones' complement): 11010101 Step 3: Add 1: 11010101 + 1 = 11010110 Verification: 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 -17: |17| = 00010001, invert = 11101110, add 1 = 11101111 Add: 00011001 + 11101111 = 100001000 Discard carry bit (9th bit): 00001000 = 8 25 + (-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).
What is the range of values for different bit widths in two's complement?
For an n-bit two's complement number, the range is from -2^(n-1) to 2^(n-1) - 1. For 8 bits (one byte), the range is -128 to 127, giving 256 total values. For 16 bits, the range is -32,768 to 32,767. For 32 bits (standard int), the range is -2,147,483,648 to 2,147,483,647 (approximately plus or minus 2.1 billion). For 64 bits (long long), the range extends to approximately plus or minus 9.2 quintillion. The asymmetry exists because zero uses one of the positive patterns, so there is one more negative value than positive. This is why the minimum value of a signed byte is -128 but the maximum is only 127.
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.
What is integer overflow and how does it relate to two's complement?
Integer overflow occurs when an arithmetic operation produces a result outside the representable range. In two's complement, adding two large positive numbers can wrap around to a negative value: in 8-bit, 120 + 10 = 130, but 130 exceeds the maximum 127, so the result wraps to -126. Similarly, subtracting from a very negative number can wrap to positive. Overflow is detected when the carry into the sign bit differs from the carry out. In C and C++, signed integer overflow is actually undefined behavior, meaning the compiler can do anything. Languages like Java guarantee two's complement wrapping behavior. Modern processors set an overflow flag that software can check to detect this condition.
How is two's complement used in networking and data protocols?
Two's complement is fundamental in network protocols and data formats. TCP/IP checksums use ones' complement arithmetic for error detection. Binary data transmitted over networks must specify byte order (big-endian or little-endian) and whether integers are signed (two's complement) or unsigned. Protocol buffers and other serialization formats use variable-length encoding schemes based on two's complement. The Internet checksum algorithm adds 16-bit words using ones' complement and takes the complement of the sum. Understanding two's complement is essential for correctly parsing binary protocols, implementing network stacks, and debugging data transmission issues across different hardware architectures.
What is sign extension and when is it needed?
Sign extension is the process of increasing the bit width of a two's complement number while preserving its value. The rule is simple: copy the sign bit (MSB) into all the new higher-order positions. For example, extending 8-bit -5 (11111011) to 16 bits gives 1111111111111011, still representing -5. Extending 8-bit +5 (00000101) to 16 bits gives 0000000000000101. Sign extension is needed whenever you assign a smaller integer type to a larger one (like byte to int), when loading narrower values from memory into wider registers, or when arithmetic requires operands of the same width. Getting sign extension wrong is a common source of bugs in systems programming.
How do you perform bitwise operations on two's complement numbers?
Bitwise AND, OR, XOR, and NOT operate on individual bits regardless of two's complement interpretation. However, the results can be surprising with signed numbers. NOT of 5 (00000101) gives 11111010, which in two's complement is -6. This follows the identity: NOT(x) = -(x+1) in two's complement. AND of two negative numbers remains negative (sign bit stays 1). XOR of a negative and positive number is negative. Left shifting by k multiplies by 2^k but can cause overflow. Arithmetic right shift preserves the sign bit (shifting in copies of MSB), while logical right shift fills with zeros. Using the wrong shift type with signed numbers is a frequent bug source in C programming.
Why is the two's complement minimum value special in programming?
The minimum value (like -128 for 8-bit or -2,147,483,648 for 32-bit) has a unique property: negating it overflows back to itself. In 8-bit, -(-128) cannot produce +128 because the maximum is only 127. The two's complement of 10000000 is 10000000 (invert to 01111111, add 1 to get 10000000). This asymmetry means that Math.abs(Integer.MIN_VALUE) produces a negative number in Java. In C, negating INT_MIN is undefined behavior. This edge case has caused real-world bugs, including a famous bug in Java's binary search implementation where (low + high) / 2 could overflow. Programmers must handle this minimum value carefully in validation and arithmetic code.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator ยท Editorial policy
Related Calculators
๐งฎOnes Complement Calculator
Calculate ones complement with inputs, formulas, and instant results.
๐งฎSet Operations Calculator
Perform union, intersection, difference, and complement operations on sets.
๐งฎAnnulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
๐งฎArea Calculator
Calculate area with inputs, formulas, and instant results.
๐งฎArea of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
๐งฎArea of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
๐งฎCenter of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.
๐งฎCentroid Calculator
Calculate centroid with inputs, formulas, and instant results.