Binary Arithmetic and Two's Complement
Add binary numbers and represent negative integers with two's complement.
Easy
Two's complement: to negate a number, invert all bits and add 1.
5 = 0000 0101
−5 = 1111 1011
An -bit signed integer ranges from to (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.