ToolSphere
Back to all articles
student

Two's Complement: Signed Binary Explained

2026-11-118 min read

Try it: free Two's Complement Calculator

Convert signed binary and trace overflow bit by bit

Open →

Digital computers represent all data using electrical switches that are either open or closed, representing binary 0 or 1. Inside a microprocessor, there is no physical "minus sign" or special negative voltage. To represent negative integers, computer architects had to devise a binary encoding that allows arithmetic logic units (ALUs) to perform addition, subtraction, and comparison using simple, high-speed logic gates.

Whether a given bit pattern like 11110011 represents the positive integer 243 or the negative integer −13 depends entirely on how the processor interprets that word. Over the history of computing, three major signed representations emerged: sign-magnitude, one's complement, and two's complement. Today, virtually every modern processor—including x86, ARM, and RISC-V—relies exclusively on two's complement.

This guide explains how all three formats work, compares their bit ranges from 4 to 32 bits, derives the negation and decoding algorithms, and walks through worked 8-bit addition, subtraction, and overflow detection traces computed with the site's digital logic library.

The Three Signed Binary Encodings

To store both positive and negative values in a fixed-width binary register of N bits, the most significant bit (MSB, bit N − 1) serves as the sign bit:

  • An MSB of 0 denotes a positive number (or zero).
  • An MSB of 1 denotes a negative number.

How the remaining bits encode magnitude differentiates the three systems.

1. Sign-Magnitude Representation

In sign-magnitude representation, the MSB indicates the sign, while the remaining N − 1 bits store the absolute magnitude as an ordinary unsigned binary number:

  • Sign bit = 0: positive value.
  • Sign bit = 1: negative value.
  • Magnitude = value of bits [N − 2 : 0].

For an 8-bit integer:

  • +13 is written as 00001101 (sign bit 0, magnitude 0001101 = 13).
  • −13 is written as 10001101 (sign bit 1, magnitude 0001101 = 13).
  • +42 is written as 00101010 (sign bit 0, magnitude 0101010 = 42).
  • −42 is written as 10101010 (sign bit 1, magnitude 0101010 = 42).

While conceptually simple, sign-magnitude has two fatal drawbacks that led hardware designers to abandon it for general-purpose integer arithmetic:

  1. Two representations of zero: +0 is encoded as 00000000, while −0 is encoded as 10000000. This wastes a bit pattern and forces software and hardware comparators to execute extra checks so that +0 == −0 evaluates to true.
  2. Complex arithmetic hardware: Adding 5 + (−3) cannot be done with a standard binary adder. The hardware must first examine the signs, compare the magnitudes, decide whether to add or subtract, and determine the sign of the result. This requires dedicated subtraction circuits and multi-cycle steering logic.

Sign-magnitude survives primarily in IEEE 754 floating-point numbers, where the sign bit, biased exponent, and fraction mantissa are handled in specialized floating-point units.

2. One's Complement Representation

In one's complement, positive numbers are written in ordinary binary. A negative number is formed by inverting every bit (bitwise NOT) of its positive counterpart:

−X = NOT(X) = (2^N − 1) − X

For an 8-bit integer:

  • +13 is 00001101. Inverting all bits gives −13 = 11110010.
  • +42 is 00101010. Inverting all bits gives −42 = 11010101.

One's complement allows addition using standard binary adders, but it suffers from two major complications:

  1. Dual zeros: +0 is 00000000, while −0 is 11111111 (all 1s).
  2. End-around carry: When adding two numbers, any carry-out generated by the most significant bit must be routed back and added into the least significant bit (LSB). This feedback loop increases addition latency and complicates pipelined ALU timing paths.

3. Two's Complement (The Modern Standard)

Two's complement solves both problems: it provides a single, unique zero and eliminates the end-around carry. In two's complement, a negative number is formed by inverting every bit and adding 1:

−X = NOT(X) + 1 = 2^N − X

For an 8-bit integer:

  • To encode −13:
    1. Write the magnitude: 00001101 (+13)
    2. Invert all bits: 11110010
    3. Add 1: 11110011
  • To encode −42:
    1. Write the magnitude: 00101010 (+42)
    2. Invert all bits: 11010101
    3. Add 1: 11010110

Negating Zero in Two's Complement

What happens when you negate zero in two's complement?

  1. Start with 00000000 (+0).
  2. Invert all bits: 11111111.
  3. Add 1: 11111111 + 1 = 100000000 (a 9-bit result).
  4. Discard the carry bit beyond the 8th bit: 00000000.

Zero negates to itself. There is no −0 pattern, eliminating duplicate-zero bugs in branch instructions and comparisons.

8-Bit Comparison Across Systems

The following table compares how common positive and negative values appear in 8-bit registers across all three systems, computed directly by the site's digital logic library:

Decimal Value Two's Complement One's Complement Sign-Magnitude
+127 01111111 01111111 01111111
+42 00101010 00101010 00101010
+13 00001101 00001101 00001101
+1 00000001 00000001 00000001
+0 00000000 00000000 00000000
−0 (None, 00000000) 11111111 10000000
−1 11111111 11111110 10000001
−13 11110011 11110010 10001101
−42 11010110 11010101 10101010
−127 10000001 10000000 11111111
−128 10000000 (Out of range) (Out of range)

Notice two critical properties of two's complement:

  1. −1 is encoded as all 1s (11111111).
  2. There is an asymmetric extra negative value: −128 (10000000). Because zero shares the positive sign bit 0, all 128 bit patterns starting with 1 are available for negative values, allowing the range to extend to −128.

Ranges Across Bit Widths (4, 8, 16, 32 Bits)

For any register width of N bits, exactly 2^N unique bit patterns exist.

  • In one's complement and sign-magnitude, the two zero representations reduce the range of distinct values to 2^N − 1, distributed symmetrically around zero:

    Range = [−(2^(N−1) − 1), +2^(N−1) − 1]
    
  • In two's complement, the unique zero allows the full 2^N patterns to be utilized:

    Range = [−2^(N−1), +2^(N−1) − 1]
    
Register Width (N) Total Patterns (2^N) Two's Complement Range One's / Sign-Magnitude Range
4-bit (nibble) 16 −8 to +7 −7 to +7
8-bit (byte) 256 −128 to +127 −127 to +127
16-bit (halfword) 65,536 −32,768 to +32,767 −32,767 to +32,767
32-bit (word) 4,294,967,296 −2,147,483,648 to +2,147,483,647 −2,147,483,647 to +2,147,483,647
64-bit (doubleword) ≈ 1.84 × 10^19 −2^63 to +2^63 − 1 −(2^63 − 1) to +2^63 − 1

The Asymmetric Minimum Integer Trap

Because the negative range extends one unit farther than the positive range, the most negative number (−2^(N−1), such as −128 in 8 bits or −2,147,483,648 in 32 bits) has no positive counterpart:

If you attempt to negate 10000000 (−128 in 8-bit):

  1. Invert bits: 01111111 (+127).
  2. Add 1: 01111111 + 1 = 10000000 (−128).

Negating −128 in an 8-bit register returns −128. In C and C++, calling abs(INT_MIN) produces signed integer overflow and undefined behavior. In Java, Math.abs(Integer.MIN_VALUE) silently returns Integer.MIN_VALUE.

Converting Between Decimal and Two's Complement

Decimal to Binary: The Right-to-Left Shortcut

You can always negate a binary number using the standard rule: invert all bits and add 1. However, an exam-tested shortcut speeds up mental and written conversions:

  1. Write the positive magnitude in binary.
  2. Read the bits from right to left (LSB to MSB).
  3. Leave all trailing zeros and the very first 1 bit unchanged.
  4. Invert every remaining bit to the left of that first 1.

Example with −40 (8-bit):

  • +40 in binary = 00101000
  • Reading right to left: trailing zeros 000, first one 1 → keep 1000.
  • Invert the remaining four bits 0010 → 1101.
  • Combined result: 11011000 (−40 in 8-bit two's complement).

Binary to Decimal: Three Decoding Methods

When given an 8-bit two's complement binary word whose MSB is 1, three equivalent methods decode it back to decimal:

Method 1: Negative Weight on the MSB (Recommended for ALUs)

In two's complement, bit positions have positive weights 2^0, 2^1, ..., 2^(N−2), but the sign bit has a negative weight of −2^(N−1).

For 8-bit 11110011:

Value = (−128 × 1) + (64 × 1) + (32 × 1) + (16 × 1) + (8 × 0) + (4 × 0) + (2 × 1) + (1 × 1)
Value = −128 + 64 + 32 + 16 + 2 + 1
Value = −128 + 115 = −13

Method 2: Subtract 2^N from the Unsigned Value

Treat the bitstring as an ordinary unsigned number, then subtract 2^N:

  • Unsigned value of 11110011 = 128 + 64 + 32 + 16 + 2 + 1 = 243.
  • Subtract 2^8 (256): 243 − 256 = −13.

Method 3: Invert and Add 1 to Find Magnitude

Negate the bitstring using two's complement to find its absolute magnitude:

  • Given: 11110011
  • Invert bits: 00001100
  • Add 1: 00001101 (decimal 13)
  • Because the original MSB was 1, prepend a minus sign: −13.

Two's Complement Addition with Carry Traces

Two's complement allows addition of signed numbers using the exact same hardware adder as unsigned numbers. The carry-out bit beyond the MSB is simply discarded.

Example 1: Positive + Positive (42 + 35 = 77)

  • A = 42 = 00101010
  • B = 35 = 00100011
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     0 1 0 0 0 1 0 0
  Operand A:    0 0 1 0 1 0 1 0   (42)
  Operand B:  + 0 0 1 0 0 0 1 1   (35)
  -----------------------------
  Sum:          0 1 0 0 1 1 0 1   (77)
  • Result: 01001101 = 64 + 8 + 4 + 1 = 77.
  • Carry into sign bit (carryIn[7]): 0.
  • Carry out of sign bit (carryOut[7]): 0.
  • Result is within [−128, +127]; no overflow occurs.

Example 2: Positive + Negative (42 + (−13) = 29)

  • A = 42 = 00101010
  • B = −13 = 11110011
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     1 1 1 0 0 1 0 0
  Operand A:    0 0 1 0 1 0 1 0   (42)
  Operand B:  + 1 1 1 1 0 0 1 1   (−13)
  -----------------------------
  Sum:          0 0 0 1 1 1 0 1   (29)
  • Result: 00011101 = 16 + 8 + 4 + 1 = 29.
  • Carry out of bit 7 is 1, which is discarded by the 8-bit register.
  • Carry into sign bit (carryIn[7]): 1.
  • Carry out of sign bit (carryOut[7]): 1.
  • Adding numbers of opposite signs can never overflow, because the magnitude of the sum is strictly smaller than the larger operand.

Example 3: Negative + Negative ((−30) + (−40) = −70)

  • A = −30 = 11100010
  • B = −40 = 11011000
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     1 1 0 0 0 0 0 0
  Operand A:    1 1 1 0 0 0 1 0   (−30)
  Operand B:  + 1 1 0 1 1 0 0 0   (−40)
  -----------------------------
  Sum:          1 0 1 1 1 0 1 0   (−70)
  • Decoding 10111010: −128 + 32 + 16 + 8 + 2 = −70.
  • Carry-out of 1 is discarded.
  • Carry into bit 7 is 1; carry out of bit 7 is 1. No overflow.

Two's Complement Subtraction: A − B as A + NOT(B) + 1

Hardware ALUs do not include a separate subtractor circuit. Subtraction A − B is executed by adding A to the two's complement negation of B:

A − B = A + (−B) = A + NOT(B) + 1

In a standard adder/subtractor circuit:

  1. Each bit of operand B is fed through a 2-input XOR gate alongside a Subtract control line.
    • When Subtract = 0: B XOR 0 = B (normal addition).
    • When Subtract = 1: B XOR 1 = NOT(B) (one's complement inversion).
  2. The Subtract control signal is wired directly into the initial carryIn of the LSB (bit 0), adding the required +1 without needing an extra clock cycle or incrementer.

Worked Subtraction: 50 − 20 = 30

  • A = 50 = 00110010
  • B = 20 = 00010100
  • NOT(B) = 11101011
  • Initial carryIn = 1
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     1 1 0 0 1 1 1 1
  Operand A:    0 0 1 1 0 0 1 0   (50)
  NOT(B):     + 1 1 1 0 1 0 1 1   (inverted 20)
  LSB carry:  +                 1
  -----------------------------
  Sum:          0 0 0 1 1 1 1 0   (30)
  • Result: 00011110 = 16 + 8 + 4 + 2 = 30.
  • Exact and stored answers match; no overflow.

Overflow Detection in Signed Arithmetic

Signed integer overflow occurs when an arithmetic operation produces a result that falls outside the representable range of the register. In an 8-bit register, any calculation resulting in a value greater than +127 or less than −128 overflows.

The Two Sign Rules for Overflow

Because numbers wrap around modulo 2^N, an overflowed sum wraps across the sign boundary:

  1. Positive Overflow: Adding two positive numbers yields an apparent negative result (MSB = 1).
  2. Negative Overflow: Adding two negative numbers yields an apparent positive result (MSB = 0).

Adding a positive number and a negative number can never cause overflow.

Worked Positive Overflow: 100 + 60 = 160

In mathematics, 100 + 60 = 160. But 160 exceeds the maximum 8-bit signed limit of +127:

  • A = 100 = 01100100
  • B = 60 = 00111100
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     1 1 1 1 1 1 0 0
  Operand A:    0 1 1 0 0 1 0 0   (100)
  Operand B:  + 0 0 1 1 1 1 0 0   (60)
  -----------------------------
  Stored bits:  1 0 1 0 0 0 0 0   (−96 in two's complement!)
  • Exact sum: 160
  • Stored result: 10100000 = −128 + 32 = −96
  • Analysis:
    • Carry into sign bit (carryIn[7]): 1
    • Carry out of sign bit (carryOut[7]): 0
    • The MSBs were both 0, but the result MSB became 1. The sum overflowed into the sign bit.

Worked Negative Overflow: (−100) + (−30) = −130

In mathematics, (−100) + (−30) = −130. But −130 falls below the minimum 8-bit signed limit of −128:

  • A = −100 = 10011100
  • B = −30 = 11100010
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     0 0 0 0 0 0 0 0
  Operand A:    1 0 0 1 1 1 0 0   (−100)
  Operand B:  + 1 1 1 0 0 0 1 0   (−30)
  -----------------------------
  Stored bits:  0 1 1 1 1 1 1 0   (+126 in two's complement!)
  • Exact sum: −130
  • Stored result: 01111110 = 64 + 32 + 16 + 8 + 4 + 2 = +126
  • Analysis:
    • Carry into sign bit (carryIn[7]): 0
    • Carry out of sign bit (carryOut[7]): 1
    • The MSBs were both 1, but the result MSB became 0. The sum underflowed below −128.

Worked Subtraction Overflow: 50 − (−90) = 140

  • A = 50 = 00110010
  • B = −90 = 10100110 → NOT(B) = 01011001
  • Exact result: 140 (exceeds +127)
  Bit index:    7 6 5 4 3 2 1 0
  Carry-in:     1 1 1 0 0 1 1 1
  Operand A:    0 0 1 1 0 0 1 0   (50)
  NOT(B):     + 0 1 0 1 1 0 0 1   (inverted −90)
  LSB carry:  +                 1
  -----------------------------
  Stored bits:  1 0 0 0 1 1 0 0   (−116)
  • Stored result: 10001100 = −128 + 8 + 4 = −116
  • carryIn[7] = 1, carryOut[7] = 0. Positive minus negative yielded a negative result.

The Golden Hardware Rule: Carry-In XOR Carry-Out

How does a CPU detect overflow in silicon in a single clock cycle without checking whether operands are positive or negative?

The ALU compares the carry bit entering the sign position against the carry bit leaving the sign position using a single XOR gate:

Overflow (V) = CarryIn[MSB] XOR CarryOut[MSB]
  • In Example 1 (42 + 35 = 77): carryIn[7] = 0, carryOut[7] = 0 → 0 XOR 0 = 0 (No overflow).
  • In Example 2 (42 + (−13) = 29): carryIn[7] = 1, carryOut[7] = 1 → 1 XOR 1 = 0 (No overflow).
  • In Positive Overflow (100 + 60 = 160): carryIn[7] = 1, carryOut[7] = 0 → 1 XOR 0 = 1 (Overflow!).
  • In Negative Overflow ((−100) + (−30) = −130): carryIn[7] = 0, carryOut[7] = 1 → 0 XOR 1 = 1 (Overflow!).

This simple hardware gate sets the Overflow Flag (V or OF in x86, ARM, and MIPS).

Overflow Flag (V) vs Carry Flag (C)

Students frequently confuse the carry flag and the overflow flag on digital logic and architecture exams:

Flag Meaning Interpreted As Where It Matters
Carry Flag (C) CarryOut[MSB] Unsigned arithmetic Did an unsigned sum exceed 2^N − 1, or did an unsigned subtraction borrow?
Overflow Flag (V) CarryIn[MSB] ⊕ CarryOut[MSB] Signed arithmetic Did a signed sum or difference fall outside [−2^(N−1), +2^(N−1) − 1]?

A CPU ALU executes the exact same binary addition regardless of whether you intend the operands to be signed or unsigned. It updates both the C flag and the V flag on every operation. The instruction chosen by the compiler or assembly programmer determines which flag to inspect:

  • For unsigned branch instructions (bgeu, bltu in RISC-V, or BHI, BLO in ARM), the CPU evaluates the Carry Flag.
  • For signed branch instructions (bge, blt in RISC-V, or BGT, BLT in ARM), the CPU evaluates the Overflow Flag combined with the Sign Flag (N ⊕ V).

Five Common Student and Exam Traps

  1. Discarding carry-out is NOT an error in signed addition: When adding 42 + (−13), a carry-out of 1 is generated. Students often assume this means the sum failed. In two's complement, carry-out from the sign bit is discarded; overflow occurs only when carryIn[MSB] ≠ carryOut[MSB].
  2. Sign extension vs. zero extension: To expand an 8-bit number to 16 bits:
    • Unsigned numbers use zero extension: prepend eight 0s (00000000 11110011 = 243).
    • Signed numbers require sign extension: copy the MSB into all upper bit slots (11111111 11110011 = −13). Zero-extending a negative number turns it into a large positive value.
  3. Assuming mixed-sign addition can overflow: The sum of a positive number and a negative number always lies between the two operands. It is mathematically impossible for mixed-sign addition to overflow.
  4. The abs(INT_MIN) trap: Never negate the most negative integer (1000...0000) without widening register width first. In 8 bits, negating −128 returns −128.
  5. Arithmetic right shift vs. logical right shift: Shifting signed values right to divide by 2 requires an arithmetic right shift (>> in C/Java, SRA in RISC-V), which replicates the sign bit. A logical right shift (>>> in Java, SRL in RISC-V) inserts 0s into the MSB, corrupting negative numbers into positive values.

Interactive Tools and Further Practice

To practice converting arbitrary decimal numbers and binary words, inspecting sign-magnitude and one's complement equivalents, or generating full carry-by-carry addition traces with overflow flags, try the free Two's Complement Calculator. For bitwise boolean logic and gate-level truth tables, check the Logic Gate Simulator and Truth Table Generator.

For a related walkthrough, read Cache Mapping Explained: Tag, Index, Offset.

Looking for other tools?

Explore ToolSphere Homepage →