sciandu
Computer science

Computer science

Binary arithmetic

Adding with just two digits: carries, shifting, and the famous 255.

What you need first

In old games, many counters could only count up to 255: more coins, more lives, more points simply did not exist. Why 255 of all numbers? The answer lies in the . You already know that computers represent everything with 0 and 1. Now you will learn to calculate with them, and along the way understand one of the most famous limits in computing.

Adding with a carry

You add in binary just like in the decimal system, except the carry already happens at 2 instead of 10. The rules per digit: 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10, so write 0 and carry 1 to the next place. Example: 101 + 11, written one above the other and aligned on the right. Rightmost place: 1 + 1 = 10, write 0, carry 1. Middle place: 0 + 1 plus the carry 1 = 10, write 0 again, carry 1. Leftmost place: 1 plus the carry 1 = 10, and since nothing follows on the left you write both digits down. Result: 1000, and indeed 5 + 3 = 8.

Bit switches
128
64
32
16
8
4
2
1

10010110

10010110 = 128 + 16 + 4 + 2 = 150

Try it: switch individual bits on and off and watch how the value of the number changes.

Doubling means shifting

In the decimal system you multiply a number by ten by appending a 0 on the right: 7 becomes 70. In binary, appending a 0 on the right doubles the number: 101 (5) becomes 1010 (10). Every digit slides one position to the left and is therefore worth twice as much. Computers love this trick because shifting is much faster for them than real multiplication.

Why 8 bits stop at 255

A has 8 bits, so 8 digits. Counting without a sign, the largest number appears when all 8 digits are set to 1: 11111111, which is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. What happens at 255 + 1? The carry travels through all the digits and would need a ninth one that does not exist. In many systems the counter then wraps around to 0, which is called overflow. Such overflows do more than reset game scores: in 1996 the Ariane 5 rocket went out of control shortly after launch because a number that was too large did not fit into a number format that was too small. With more bits or a sign the limits look different, but for unsigned 8 bits they end at 255.

Exercises

0 of 6 solved

Time to try it yourself. You can't break anything, every attempt counts.

What is 1 + 1 in binary?

What decimal number is the binary number 101?

Calculate in binary: 110 + 1. Enter the result as a binary number.

Order the binary numbers from smallest to largest.

  1. 11000
  2. 210
  3. 3100
  4. 4101

Calculate in binary: 101 + 11. Enter the result as a binary number.

A byte with 8 bits can store at most the number without a sign.

Where this leads