Skip to content
UPEEC

Binary Arithmetic and Two's Complement

Add binary numbers and represent negative integers with two's complement.

Easy
Comments 0

Two's complement: to negate a number, invert all bits and add 1.

 5  = 0000 0101
−5  = 1111 1011

An nn-bit signed integer ranges from −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1 (8-bit: −128 to 127). Addition works the same for signed and unsigned numbers — that's why every CPU uses two's complement.

Overflow happens when the result doesn't fit: in 8-bit signed arithmetic, 100 + 100 = −56.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.