7.2 r's and (r-1)'s Complements (1's & 2's Complement)
💡 Core Intuition: The Clock Face & Turning Backwards by Moving Forwards
Why does every microprocessor on Earth use complements instead of ordinary subtraction?
Building physical silicon subtraction circuits (borrow logic, borrow-propagation trees) requires almost twice the silicon real estate of an adder, drastically increasing chip die area, parasitic capacitance, and power consumption. Complements allow the CPU to perform subtraction using purely additive hardware!
🍳 The Everyday Analogy: The 12-Hour Wall Clock
Suppose your watch displays 9:00, and you need to set it back by 4 hours (). You have two equivalent choices:
- Rotate the dial counter-clockwise by 4 hours: .
- Rotate the dial clockwise by 8 hours: .
Because the clock dial wraps around modulo 12, subtracting 4 is mathematically identical to adding 8 (the 12's complement of 4)!
In digital hardware with an -bit register, numbers naturally wrap around modulo . By substituting negative numbers with their radix complement, the Arithmetic Logic Unit (ALU) performs subtractions using the exact same silicon adder circuits that execute standard additions!
💻 Bridging to Computer Science
In processor architecture and compiler runtimes:
- Unified Arithmetic Hardware: An ALU does not need separate adder and subtractor silicon circuits. Subtraction is executed as on the exact same adder datapath.
- Elimination of Negative Zero: 1's complement has two representations for zero ( and ), requiring extra logic in conditional branches (
if (x == 0)). 2's complement has exactly one unique zero (), simplifying CPU control logic. - Asymmetric Dynamic Range: In an -bit 2's complement system, the range is . The extra negative value (e.g. in 8-bit) explains why integer negation can overflow in C/C++ (
abs(INT_MIN)undefined behavior).
📑Table of Contents
- 📐 Mathematical Foundations of Complements
- 🔟 Diminished Radix Complement: (r-1)'s Complement
- ⚡ Radix Complement: r's Complement
- 🚀 Fast Silicon Shortcuts (Bit Inversion & RTL Scan)
- ➕ Subtraction via Addition (Positive vs. Negative Results)
- 🎛️ Interactive Complements & Subtraction Visualizer
- ⚔️ Architectural Showdown: 1's Complement vs. 2's Complement
- 🏭 Real-World Production Context: ALU Hardware Simplicity & Zero-Cost Subtraction
- 📐 Numerical Applications & Worked Examples
- 🎯 Exam & Interview Pitfall Check
📐 Mathematical Foundations of Complements
For any positional number system with base (radix) , there exist two distinct families of complements:
- The Radix Complement ('s complement):
- In Decimal (): 10's complement
- In Binary (): 2's complement
- The Diminished Radix Complement ('s complement):
- In Decimal (): 9's complement
- In Binary (): 1's complement
The fundamental identity linking the two families is:
🔟 Diminished Radix Complement: 's Complement
Given an -digit integer in radix :
If contains fractional digits:
Notice that is simply a number consisting of repetitions of the maximum digit !
- In Decimal (), . To find the 9's complement, simply subtract every digit from .
- In Binary (), . To find the 1's complement, simply subtract every bit from .
Decimal 9's Complement Example
Find the 9's complement of ( digits):
Binary 1's Complement Example
Find the 1's complement of ( bits):
Notice the physical rule: Subtracting a binary bit from 1 simply inverts the bit ( and )! In silicon, 1's complement is implemented with zero calculation: simply pass each bit through a single NOT gate inverter!
⚡ Radix Complement: 's Complement
Given an -digit integer in radix (with ):
(For , the 's complement is defined as .)
Decimal 10's Complement Example
Find the 10's complement of :
- Method 1 (Definition): .
- Method 2 (): .
Binary 2's Complement Example
Find the 2's complement of :
- Method 1: Invert all bits (1's comp) and add 1:
🚀 Fast Silicon Shortcuts (Bit Inversion & RTL Scan)
In high-speed hardware and competitive timed exams, computing with carry ripple is slow. Two high-speed shortcuts exist:
The Right-to-Left (RTL) Bit Scanning Shortcut
To find the 2's complement of any binary vector:
- Scan the bits from Right to Left (LSB toward MSB).
- Leave all trailing zeros unchanged.
- Leave the first '1' bit encountered unchanged.
- Invert every bit to the left of that first '1' ()!
Right-to-Left (RTL) Bit Scanning Shortcut Pipeline
O(1) inspection method for finding the 2's complement of any binary vector
Step 1: Scan Right-to-Left
LSB → MSBBegin at the least significant bit (LSB) and scan leftward toward MSB.
Scan direction: ← (from bit 0)Step 2: Copy Trailing Zeros Unchanged
Unchanged BitsAll consecutive trailing zeros before the first 1-bit remain unmodified.
Original: ...00 → Output: ...00Step 3: Copy First 1-Bit Unchanged
Pivot PointThe very first encountered logic 1 is copied down without alteration.
Original: ...100 → Output: ...100Step 4: Bitwise Invert All Remaining Bits to the Left
Inversion PhaseEvery bit strictly to the left of the pivot 1 is flipped (0 → 1, 1 → 0).
Original: 10110... → Output: 01001...
This single rule enables a hardware carry-chain or human engineer to evaluate 2's complement in visual inspection!
➕ Subtraction via Addition (Positive vs. Negative Results)
Suppose an ALU needs to compute for two -digit positive numbers using an adder.
Subtraction via Complements Decision Framework (M - N)
Hardware execution rules based on End Carry generation across 1's and 2's complement ALUs
Case 1: M ≥ N (End Carry = 1)
- •In 2's (r's) Complement: Simply DISCARD the end carry. The remaining n bits are already the true positive magnitude.
- •In 1's (r-1's) Complement: Add End-Around Carry (EAC) back to the LSB position (M + N' + 1).
- •Hardware Benefit: In 2's complement, no second addition step is required! Discarding the carry wire is instantaneous (zero delay).
Case 2: M < N (End Carry = 0)
- •Indicates the true result is negative. The raw sum bits are in COMPLEMENT form.
- •In 2's (r's) Complement: Take 2's complement of the sum to reveal the true magnitude, then prepend a '-' sign.
- •In 1's (r-1's) Complement: Take 1's complement of the sum to reveal the true magnitude, then prepend a '-' sign.
- •No End-Around Carry is generated or added.
ALU Hardware Subtraction Pipeline
Step-by-step arithmetic path executed by adder-subtractor units
Step 1: Complement Subtrahend
Form Comp(N)Invert all bits of N (1's comp) or invert and assert initial carry Cin = 1 (2's comp).
N* = N' (for 1's) or N' + 1 (for 2's)Step 2: Binary Addition
Sum EvaluationFeed minuend M and complemented N* into n-bit binary full adder array.
Sum = M + N*Step 3: End Carry Inspection
Branch DecisionInspect carry-out terminal Cout: If Cout = 1, result is positive. If Cout = 0, result is negative in complement form.
Cout = 1 → M ≥ N | Cout = 0 → M < NStep 4: Final Normalization
Final AnswerIf Cout=1 in 2's comp: discard Cout. If Cout=1 in 1's comp: add EAC to LSB. If Cout=0: take complement of sum and prepend negative sign.
Result = Discard Cout | Add EAC | Invert Sum (-)Case 1: Subtraction when Minuend Exceeds Subtrahend (Positive Result)
When the minuend is greater than or equal to the subtrahend (), the true arithmetic difference is non-negative (). In hardware complement arithmetic, adding the complement of generates an overflow end carry (), confirming that the result is strictly positive and requires no secondary complementation.
A. Using 2's Complement ('s complement)
- Compute .
- An End Carry-Out () will be generated.
- Discard the End Carry! The remaining bits represent the exact positive answer.
Problem: Compute in 8-bit binary.
- 2's Comp of
- End Carry Discard carry!
- Result . (Correct!)
B. Using 1's Complement ('s complement)
- Compute .
- An End Carry-Out () will be generated.
- Add the Carry to the Least Significant Bit (LSB)! This is called the End-Around Carry (EAC).
Problem: Compute in 8-bit binary using 1's complement.
Case 2: Subtraction when Subtrahend Exceeds Minuend (Negative Result)
When , adding the complement does not generate an End Carry ().
- The absence of an end carry indicates that the result is negative.
- The bit pattern obtained is not the true magnitude; it is in complement form.
- To obtain the true magnitude, take the complement of the sum and prefix a minus sign!
Example: Compute in 8-bit binary using 2's complement
- 2's Comp of
- No End Carry Result is negative and stored in 2's complement form ().
- Take 2's complement of :
- Final Result: . (Correct!)
🎛️ Interactive Complements & Subtraction Visualizer
Experiment live with 4-bit, 8-bit, and 16-bit operands. Watch the hardware adder columns execute, observe carry propagation, inspect the End-Around Carry vs Discard rule, and test signed overflow detection:
⚔️ Architectural Showdown: 1's Complement vs. 2's Complement
Why did the computer industry (from the CDC 6600 in the 1960s to modern Intel Core i9 and Apple M-series chips) completely abandon 1's complement in favor of 2's complement?
🏭 Real-World Production Context: ALU Hardware Simplicity & Zero-Cost Subtraction
In real silicon microprocessors, complements eliminate the need for distinct subtraction circuits.
Microprocessor Arithmetic Datapath Subtraction Stack
Signed Primitive Types (C/C++, Rust, Java)
Compilers map signed variables (int32_t, int64_t) into 2's complement bit patterns with asymmetric ranges [-2^(n-1) to +2^(n-1)-1].
ALU Control Signals (Mode M = 1)
The control unit decodes the SUB opcode, asserting the subtraction control wire (M=1) across the datapath.
Unified Adder-Subtractor (XOR Bank + Adder)
Input B passes through XOR gates with M, generating 1's complement. LSB Cin=1 adds 1, completing 2's complement on the fly.
ALU Status Register (Carry & Overflow Flags)
The ALU updates condition flags: Carry flag C reflects unsigned borrow, while Overflow flag V signals signed 2's complement overflow.
- The Unified Adder-Subtractor: Modern CPUs (x86, ARM, RISC-V) perform subtraction with zero added adder hardware. A bank of XOR gates conditionally inverts input when subtraction mode () is asserted, while feeding into the LSB full adder.
- Instruction Set Encoding: All modern programming languages (C, C++, Rust, Go, Java, Python) standardize on 2's complement for signed integer types (
int8_t,int16_t,int32_t,int64_t), ensuring hardware-level binary compatibility.
📐 Numerical Applications & Worked Examples
Example 1: Decimal Subtraction Using 9's and 10's Complements (Both Cases)
Problem Statement:
Perform the subtraction of 5-digit decimal numbers and :
- Compute using 10's complement (Case 1: ).
- Compute using 10's complement (Case 2: ).
Step-by-Step Solution:
- Case 1: Compute using 10's Complement:
- 9's complement of :
- 10's complement of :
- Add :
- An End Carry-Out () is generated, indicating (result is positive).
- Discard the end carry: Final Answer = .
- Case 2: Compute using 10's Complement:
- 9's complement of :
- 10's complement of :
- Add :
- No carry generated (), indicating (result is negative and in 10's complement form).
- Take 10's complement of the sum : .
- Prepend negative sign: Final Answer = .
Example 2: 1's vs 2's Complement Subtraction in 6-Bit Binary
Problem Statement:
Perform and in 6-bit binary arithmetic using:
- 1's complement representation.
- 2's complement representation.
Step-by-Step Solution:
- Represent Numbers in 6-Bit Binary:
- Compute in 1's Complement:
- 1's complement of : invert all bits .
- Add :
- End carry generated! In 1's complement, add End-Around Carry (EAC) to LSB:
- Compute in 2's Complement:
- 2's complement of .
- Add :
- End carry generated! In 2's complement, discard carry:
(Notice: instantaneous zero extra gate delay!).
- Compute in 2's Complement:
- 2's complement of .
- Add :
- No carry generated negative result in 2's complement form.
- Take 2's complement of sum :
- Final Answer: .
Example 3: Complements of Fractional Numbers & Radix Extension
Problem Statement:
Find the diminished radix complement and radix complement for the fractional number and binary fraction . State the generalized mathematical formula for fractional complements.
Step-by-Step Solution:
- Generalized Mathematical Formulas for Fractions:
For a number with integer digits and fractional digits:- Diminished Radix Complement:
- Radix Complement:
- Diminished Radix Complement:
- Compute for Decimal Fraction ():
- 9's complement: subtract each digit from :
- 10's complement: add to LSB:
- Direct verification: .
- 9's complement: subtract each digit from :
- Compute for Binary Fraction ():
- 1's complement: invert all bits after the binary point:
- 2's complement: add to LSB:
- Direct verification via RTL scan shortcut: scanning from right, trailing zero is , first 1 is , all bits to the left are inverted .
- 1's complement: invert all bits after the binary point:
🎯 Exam & Interview Pitfall Check
- Why does 2's complement have one extra negative number than positive numbers?
In an -bit system, there are unique bit combinations. Zero () takes one combination, leaving states (an odd number). Because zero is assigned to the non-negative group (its MSB is ), there are positive numbers ( to ) and negative numbers ( to ). - What happens if you take the 2's complement of the most negative number (e.g. in 8-bit)?
In 8-bit 2's complement, is represented as .
Inverting bits yields .
Adding 1 yields: (which is again!).
Because cannot fit in 8-bit signed two's complement, the operation triggers an Arithmetic Overflow!
- Trap: Forgetting that End-Around Carry applies ONLY to 1's complement, NEVER 2's complement.
In 2's complement subtraction, if an end carry appears, throw it away. Never add it back to the LSB! - Trap: Misidentifying the result of Case 2 ().
When , students frequently read the bit pattern directly as the magnitude. Remember: the sum is in complement form. You must complement the sum to obtain the true magnitude, then write a negative sign in front!