Fundamentals 11 min read

Why Do Computers Use Two’s Complement? The Story Behind Binary Number Representation

This article explains why computers use binary and two's complement to represent signed numbers, tracing the evolution from counting on fingers to decimal, Roman numerals, original code, one's complement, and finally two's complement, and shows how these choices simplify hardware design.

ITPUB
ITPUB
ITPUB
Why Do Computers Use Two’s Complement? The Story Behind Binary Number Representation

Number bases and positional notation

Humans use a decimal (base‑10) system because we have ten fingers. If a species had four fingers, a natural numeral system would be base‑4. Computers, however, are built from binary switches, so they operate in base‑2.

Positional notation assigns a weight to each digit. The digit 0 is essential because it marks an empty place value. Arabic numerals (e.g., 205) use positional notation, whereas Roman numerals lack a zero symbol and cannot represent large numbers compactly.

Binary representation of unsigned integers

With k bits you can encode 2^k distinct values, ranging from 0 to 2^k‑1. For example, an 8‑bit unsigned integer represents the interval 0 … 255. The binary form of decimal 5 is 101.

Signed integer representations

To encode negative numbers a sign bit is required. Three classic schemes are described below.

Sign‑magnitude (original code)

The most intuitive method copies the magnitude bits and adds a sign bit: 0 010 = +2, 1 010 = –2. This is called sign‑magnitude or original code . It suffers from a duplicate representation of zero ( 0000 and 1000), i.e., a distinct “‑0”.

One's complement (反码)

One's complement is obtained by flipping every bit of the positive representation. Example: 0010 (+2) → 1101 (‑2). This also yields a “‑0” representation ( 1111), so the duplicate‑zero problem remains.

Two's complement (补码)

Two's complement eliminates the “‑0” by defining the negative representation as the bitwise complement of the positive value plus one. For a 4‑bit word, –2 is represented as 1110 because 0010 (2) → complement 1101 → add 1 → 1110. Adding 0010 (+2) and 1110 (‑2) yields 0000 with an overflow that is discarded, giving the correct result. With k bits, two's complement can represent the range -2^{k‑1} … 2^{k‑1}‑1 . For 4 bits this is -8 … +7 .

Why two's complement works in hardware

The same binary adder circuitry can be used for both signed and unsigned addition. The ALU simply adds the bit patterns and discards any carry out of the most‑significant bit. No extra logic is needed to treat the sign specially, which greatly simplifies circuit design and reduces transistor count. During addition the carry generated by the most‑significant bit is ignored. For example, adding 1111 (‑1) and 0001 (+1) produces 10000 ; discarding the leading 1 leaves 0000 , the correct zero result.

Practical implications

Programmers need only know the numeric range of a given type; the compiler and hardware handle the two's‑complement encoding automatically.

Because addition, subtraction, and even multiplication work with the same binary circuits, arithmetic operations are fast and area‑efficient.

Sign‑magnitude and one's‑complement require additional circuitry to detect and correct the “‑0” case, which historically made them less attractive.

Summary

Two's complement provides a compact, unambiguous representation of signed integers, enables seamless arithmetic using the same binary adder as for unsigned numbers, and eliminates the need for special handling of a negative zero. Consequently, modern CPUs adopt two's complement despite its counter‑intuitive mapping for humans.

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.

computer architectureBinarytwo's-complementnumber systemssigned integers
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.