Courses / Computer Science I
Introduction to Computers

Arithmetic at Machine Level

Computer Science I 287 words Free to read

Doing Sums in Binary

Computers add binary numbers with the same carry rules as decimal, but with only two digits. The core facts are 0+0=00+0=0, 0+1=10+1=1, 1+0=11+0=1, and 1+1=101+1=10 (that is, 00 carry 11). Adding 011+001011 + 001 gives 100100: the rightmost column 1+11+1 is 00 carry 11, the next column 1+0+carry 11+0+\text{carry }1 is 00 carry 11, and the last column receives that carry to give 11.

Because a register holds a fixed number of bits, a sum that needs more bits than are available overflows — the extra high bit is lost. In an 8-bit register, 255+1255 + 1 wraps around to 00, because the true answer 256256 needs a 9th bit that does not exist. Overflow is not an error the hardware raises by default; it is silent, and reasoning about it is a real part of low-level programming.

Negative numbers use two's complement, the near-universal scheme for signed integers. To negate a number, invert all its bits and add 1. In 8-bit two's complement, the leftmost bit is the sign bit: 00 means non-negative, 11 means negative. This representation has one great virtue: the ordinary binary addition circuit works correctly for signed numbers too, with no special cases — subtraction becomes "add the two's complement."

8-bit patternTwo's-complement value
0000000100000001+1+1
11111111111111111-1
1000000010000000128-128
0111111101111111+127+127
Common pitfall: assuming an integer can grow without bound. Machine integers have a fixed width, so they wrap on overflow — an 8-bit unsigned value silently goes 2550255 \to 0, and a signed one goes +127128+127 \to -128. Forgetting this fixed range is the source of countless real-world bugs.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes
Start Computer Science I free

Introduction to Computers