Two's Complement Calculator – Convert to Twos Complement
Convert any integer to its two's complement binary form or decode two's complement back to decimal with our free online calculator. Supports 8-bit, 16-bit, and 32-bit representations.
How Two's Complement Works
Two's complement is the standard method computers use to represent signed integers in binary. It allows the same hardware circuits to perform addition and subtraction on both positive and negative numbers, making it essential for computer arithmetic.
To convert a positive number: Simply write it in binary and pad to the desired bit width. The leftmost bit (sign bit) will be 0.
To convert a negative number:
- Start with the absolute value in binary
- Invert all bits (0→1, 1→0) to get the one's complement
- Add 1 to get the two's complement
The leftmost bit indicates the sign: 0 for positive, 1 for negative. In an 8-bit system, you can represent numbers from -128 to +127.
Example Conversions
Converting +42 to 8-bit Two's Complement
Step 1: Convert 42 to binary
42 ÷ 2 = 21 remainder 0
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
42 in binary = 101010
Pad to 8 bits: 00101010
Converting -42 to 8-bit Two's Complement
Step 1: Start with |−42| = 42 = 00101010
Step 2: Invert all bits (one's complement)
00101010 → 11010101
Step 3: Add 1
11010101 + 1 = 11010110
So -42 in 8-bit two's complement = 11010110
Converting Two's Complement Back to Decimal
Decode: 11010110 (8-bit)
Step 1: Check sign bit (leftmost)
1 = negative number
Step 2: Invert all bits
11010110 → 00101001
Step 3: Add 1
00101001 + 1 = 00101010
Step 4: Convert to decimal and negate
00101010 = 42, so result = -42
16-bit Example: -1000
1000 in binary = 1111101000
Pad to 16 bits: 0000001111101000
Invert: 1111110000010111
Add 1: 1111110000011000
-1000 in 16-bit = 1111110000011000 = 0xFC18
Quick Fact: Why Two's Complement Won
Early computers used different methods for negative numbers. The IBM 704 (1954) used one's complement, which had a strange quirk: it had both +0 (00000000) and -0 (11111111)! This wasted a representation and complicated arithmetic. Two's complement was proposed by John von Neumann in 1945 and has only one zero, simpler arithmetic circuits, and the same addition hardware works for both positive and negative numbers. By the 1970s, two's complement became universal. The PDP-8 minicomputer helped popularize it, and today every modern processor uses two's complement for integer arithmetic.
Frequently Asked Questions
Why do computers use two's complement?
Two's complement allows the same addition circuit to handle both positive and negative numbers. For example, 5 + (-3) works the same as regular binary addition. This simplifies hardware design and improves speed. It also avoids the problem of having both +0 and -0.
What's the range of an 8-bit two's complement number?
An 8-bit two's complement can represent -128 to +127. Notice the asymmetry: there's one more negative number than positive. This is because 0 takes up one of the "positive" patterns (00000000). For n bits, the range is -2^(n-1) to 2^(n-1)-1.
What does the sign bit tell me?
The leftmost bit indicates the sign: 0 means positive (or zero), 1 means negative. In 8-bit, any number starting with 0 (00000000 to 01111111) is 0 to 127. Any number starting with 1 (10000000 to 11111111) is -128 to -1.
How do I detect overflow in two's complement?
Overflow occurs when adding two positives gives a negative, or adding two negatives gives a positive. More precisely: overflow happens when the carry into the sign bit differs from the carry out of the sign bit. For 8-bit: 100 + 50 = 150 overflows because 150 > 127.
What is one's complement and why isn't it used?
One's complement simply inverts all bits to negate a number. The problem: it has two representations of zero (00000000 and 11111111), which complicates comparisons. Also, addition requires an "end-around carry" step. Two's complement fixes both issues.
How do I convert two's complement to hexadecimal?
Group the binary digits into sets of 4 (starting from the right), then convert each group to hex. For example, 11010110 becomes 1101 0110 = D6 in hex. The hex value 0xD6 represents -42 in 8-bit two's complement.
What happens if I try to represent -129 in 8-bit?
You can't—this causes overflow. The minimum 8-bit value is -128 (10000000). Trying to represent -129 would wrap around to +127 due to overflow. This is why choosing the right bit width matters: use 16-bit for values outside -128 to 127.
Other Free Tools
Binary Addition & Subtraction Calculator – Compute in Base 2
Add and subtract binary numbers step by step with our free online binary calculator. See each bit-by-bit operation clearly – perfect for computer science and digital electronics.
Bitwise Operations Calculator – Compute AND OR XOR NOT Shifts
Perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations on integers with our free online bitwise calculator. See binary representations alongside results.
Number Base Converter – Binary, Octal, Decimal, Hex Converter
Convert numbers between binary, octal, decimal, and hexadecimal bases instantly with our free online number base converter. Perfect for computer science students and programmers.
Truth Table Generator – Create Logic Truth Tables Online
Generate truth tables for any logical expression with our free online truth table generator. Supports AND, OR, NOT, XOR, NAND, NOR, and implication operators for any number of variables.
Boolean Expression Evaluator – Evaluate Logic Expressions Online
Evaluate any Boolean expression for given variable values with our free online Boolean expression evaluator. Supports all logical operators including AND, OR, NOT, XOR, and more.
Logic Gate Simulator – Simulate AND OR NOT Gates Online
Simulate any combination of digital logic gates with our free online logic gate simulator. Set input values and see real-time output for AND, OR, NOT, NAND, NOR, and XOR gates.
Data Storage Converter – Convert KB, MB, GB, TB Online
Convert between any digital storage unit with our free online data storage converter. Supports bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes instantly.
BODMAS / PEMDAS Calculator – Order of Operations Solver
Solve any math expression using the correct order of operations with our free BODMAS/PEMDAS calculator. Get step-by-step breakdowns to understand exactly how each expression is evaluated.