Fundamentals 12 min read

Why Computers Use Two’s Complement: Unraveling Original, Inverse, and Complement Codes

This article explains machine numbers versus true values, defines original code, one's complement, and two's complement, and shows how modular arithmetic lets computers replace subtraction with addition, eliminating duplicate zeros and simplifying hardware design.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Computers Use Two’s Complement: Unraveling Original, Inverse, and Complement Codes

Machine Numbers and True Values

Before discussing original code, one's complement and two's complement, we must distinguish between a machine number (the signed binary pattern stored in memory) and its true value (the mathematical integer it represents). The most‑significant bit is the sign bit: 0 for positive, 1 for negative.

Example: with an 8‑bit word, +3 is 00000011, –3 is 10000011. The machine number 10000011 has a true value of –3, not the unsigned value 131.

Original Code (Sign‑Magnitude)

Original code stores the sign bit unchanged and the magnitude in the remaining bits. For 8‑bit numbers:

+1 → 00000001 –1 → 10000001

The representable range is –127 to +127. Original code is easy for humans to read but unsuitable for arithmetic because the sign bit participates in addition.

One’s Complement

Positive numbers keep the same pattern; negative numbers invert every bit except the sign bit.

+1 → 00000001 –1 → 11111110

Because the sign bit still indicates negativity, the result of adding a negative number must be converted back to original code for interpretation, and there exist two representations of zero (positive and negative).

Two’s Complement

Negative numbers are formed by inverting all bits of the original code (except the sign bit) and then adding 1.

+1 → 00000001 –1 → 11111111

Two’s complement eliminates the duplicate zero and extends the range to –128 to +127 for an 8‑bit word. It also allows subtraction to be performed as addition, simplifying hardware design.

Why Use Complement Representations?

Using only addition circuits, a computer can compute subtraction by adding the complement of the subtrahend. Original code fails because the sign bit participates in addition, producing incorrect results (e.g., 1 – 1 yields –2). One’s complement fixes the subtraction result but still has two zeros. Two’s complement resolves both issues and provides a unique representation for the most negative value.

Mathematical Insight: Modular Arithmetic

The complement operations correspond to arithmetic modulo 2ⁿ, where n is the word length. For an 8‑bit word, adding a negative number is equivalent to adding its modulo‑256 equivalent. Examples:

–1 ≡ 255 (mod 256) 2 – 1 = 2 + (–1) ≡ 2 + 255 = 257 ≡ 1 (mod 256)

This explains why the complement of –1 is 255 (binary 11111111) and why overflow wraps around the “clock face” of the word size.

Practical Implications

Modern programming languages represent signed integers using two’s complement, so a 32‑bit int ranges from –2³¹ to 2³¹ – 1. Understanding these representations is essential for low‑level debugging, bit‑wise algorithms, and avoiding overflow bugs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Binarytwo's-complementmodular arithmeticnumber representation
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.