MATHEMATICSNumber SystemsMathematics Calculator
๐Ÿ”ข

Two's Complement

Two's complement is the standard signed integer representation: invert all bits and add 1. Range for n bits: -2^(n-1) to 2^(n-1)-1. Used by every modern CPU.

Concept Fundamentals
Flip bits + 1
Method
-2^(n-1) to 2^(n-1)-1
Range
Single representation
Zero
A - B = A + (-B)
Subtraction

Did our AI summary help? Let us know.

Every CPU uses two's complement for signed integers. 127 + 1 = -128 in 8-bit (overflow wraps). A - B = A + (-B); one circuit for add and subtract.

Key quantities
Flip bits + 1
Method
Key relation
-2^(n-1) to 2^(n-1)-1
Range
Key relation
Single representation
Zero
Key relation
A - B = A + (-B)
Subtraction
Key relation

Ready to run the numbers?

Why: Two's complement enables one adder to handle both addition and subtraction. No negative zero; symmetric range except one extra negative.

How: Invert every bit (one's complement), then add 1. The MSB is the sign bit.

Every CPU uses two's complement for signed integers.127 + 1 = -128 in 8-bit (overflow wraps).

Run the calculator when you are ready.

Two's Complement ConversionEnter value in binary, decimal, or hex
2
MODERN SIGNED BINARY

Two's Complement โ€” Modern Signed Binary

Flip bits + add 1. Used by every CPU. Range -2^(n-1) to 2^(n-1)-1. No negative zero.

Input Format

Input & Bit Width

For educational and informational purposes only. Verify with a qualified professional.

๐Ÿงฎ Fascinating Math Facts

โšก

Flip bits + 1 = two's complement

๐Ÿ“

MSB = sign bit: 0 positive, 1 negative

๐Ÿ“‹ Key Takeaways

  • โ€ข Two's complement = flip all bits + add 1 (one's complement + 1)
  • โ€ข No negative zero โ€” single representation of zero (unlike one's complement)
  • โ€ข For n-bit signed integers, range is -2^(n-1) to 2^(n-1)-1 (e.g., 8-bit: -128 to +127)
  • โ€ข Used by every modern CPU for signed integer arithmetic; Java, C, JavaScript all use it

๐Ÿ’ก Did You Know?

๐Ÿ–ฅ๏ธEvery modern CPU uses two's complement for signed integers โ€” x86, ARM, RISC-V, MIPSSource: Computer Architecture
โ˜•Java's int and long types are defined as two's complement in the JLS specificationSource: Java Language Spec
๐Ÿ”„Overflow wraps around: 127 + 1 = -128 in 8-bit. The carry is discarded.Source: Integer Arithmetic
๐Ÿง John von Neumann popularized two's complement in the 1940s for early computersSource: Computer History
โž–Subtraction = addition: A - B = A + (-B). CPUs use one adder for both add and subtractSource: ALU Design
๐Ÿ“The MSB (most significant bit) is the sign bit: 0 = positive/zero, 1 = negativeSource: Signed Representation
๐Ÿ”ขIn 8-bit: -1 = 11111111. Taking two's complement of -1 gives +1 (00000001)Source: Double Negation

๐Ÿ“– How Two's Complement Works

Two's complement is the standard way computers represent signed integers. To find the two's complement of a binary number: (1) Invert every bit (one's complement), then (2) Add 1.

Example: -5 in 8-bit

5 = 00000101 โ†’ invert โ†’ 11111010 โ†’ add 1 โ†’ 11111011. So -5 = 11111011.

Why Add 1?

Adding 1 eliminates the "negative zero" problem of one's complement. It also makes the range symmetric (except for one extra negative value: -128 in 8-bit).

Subtraction via Addition

A - B = A + (-B). The CPU computes -B as two's complement of B, then adds. One circuit handles both operations.

๐ŸŽฏ Expert Tips

Quick Negation

In C/Java: -x is two's complement. Bitwise: ~x + 1. JavaScript: -x for numbers.

Overflow Detection

Signed overflow: result sign differs from operands. 127 + 1 = -128 (overflow in 8-bit).

Bit Width Matters

-42 in 8-bit = 11010110. In 16-bit = 1111111111010110. Always specify bit width.

Minimum Negative

-128 in 8-bit is 10000000. Its two's complement is itself (special case).

โš–๏ธ Two's Complement vs One's Complement vs Sign-Magnitude

FeatureTwo's ComplementOne's ComplementSign-Magnitude
OperationFlip bits + 1Flip bits onlyMSB = sign, rest = magnitude
Zero representationSingle zero+0 and -0+0 and -0
8-bit range-128 to +127-127 to +127-127 to +127
HardwareNOT + adderNOT gates onlySign bit + magnitude
Modern usageStandard (all CPUs)Checksums, DSPLegacy, IEEE float sign

โ“ Frequently Asked Questions

What's the difference between one's and two's complement?

One's complement = invert all bits. Two's complement = invert all bits + 1. Two's has a single zero and is the standard for signed integers in all modern computers.

Why does two's complement have one extra negative value?

For 8-bit: range is -128 to +127. There are 128 negative values but only 127 positive (plus zero). The bit pattern 10000000 represents -128, which has no positive counterpart.

How do I convert a negative decimal to two's complement?

Take the absolute value, convert to binary, then take two's complement (flip bits + 1). Or: add 2^n to the negative number and convert to binary.

What happens when I add 1 to the maximum positive value?

Overflow. 127 + 1 = -128 in 8-bit. The result wraps around. This is why overflow detection is important in critical applications.

Does Java use two's complement?

Yes. The Java Language Specification defines int and long as two's complement. Same for C, C++, JavaScript (for bitwise ops), and virtually all modern languages.

What is the two's complement of zero?

Zero. 00000000 โ†’ flip โ†’ 11111111 โ†’ add 1 โ†’ 100000000 (9 bits). Discard overflow โ†’ 00000000. So -0 = 0 in two's complement.

Why was two's complement invented?

It simplifies hardware: one adder handles both addition and subtraction. It eliminates the negative zero problem. John von Neumann advocated for it in early computer design.

How do I detect signed overflow?

When adding two positives and getting negative, or two negatives and getting positive. The sign of the result differs from what you'd expect.

What is the range for 32-bit two's complement?

-2,147,483,648 to 2,147,483,647. This is the range of Java int and C int on most systems.

๐Ÿ“Š Two's Complement by the Numbers

2
Steps (flip+1)
0
Negative Zero
5
Bit Widths
100%
CPU Adoption

โš ๏ธ Disclaimer: This calculator provides two's complement conversion for educational purposes. JavaScript bitwise operations use 32-bit. Always specify bit width when working with binary in different contexts. Verify with official language specifications for production code.

๐Ÿ‘ˆ START HERE
โฌ…๏ธJump in and explore the concept!
AI

Related Calculators