Math for Programming / Part 2: Math inside the computer

Binary: counting with on and off

Every number, color, letter, and sound in a computer is stored as bits. Place value, the idea you learned with ones, tens, and hundreds, explains all of it.

Lesson 8 of 13About 12 minutesBuilds on: place value and exponents

Place value, with only two digits

In ordinary base-10 numbers, each place is worth ten times the place to its right: 345 means 3 hundreds, 4 tens, and 5 ones. Computers store everything with switches that are either off or on, so they count in base 2, called binary. Each place is worth twice the place to its right: ones, twos, fours, eights, sixteens, and so on. Each binary digit is a bit.

Worked example

Binary 1011 means 1 eight, 0 fours, 1 two, and 1 one: 8 + 0 + 2 + 1 = 11. Going the other way, 13 = 8 + 4 + 1, so it is 1101 in binary.

Figure

Click the bits to switch them on and off.

How big can a number get?

With n bits there are 2ⁿ possible patterns. A group of 8 bits, a byte, has 2⁸ = 256 patterns, so it can store the whole numbers 0 through 255. Sixteen bits give 65,536 patterns. Thirty-two bits give about 4.3 billion. Every extra bit doubles the count, the same doubling you see in the legend of rice grains on a chessboard.

Limits have consequences. If an unsigned byte holding 255 is increased by 1, it wraps around to 0, much like a car odometer rolling over. Old video games are full of famous glitches caused by numbers that outgrew their bits.

Hexadecimal: binary in a shorter form

Long strings of bits are hard to read, so programmers group them four at a time. Four bits have 16 patterns, written with the digits 0–9 and then A–F (A = 10, B = 11, … F = 15). This is base 16, or hexadecimal. One byte is always exactly two hex digits: 255 is 1111 1111 in binary and FF in hex.

Reading a color code

Web and game colors are often written like #FF8800: two hex digits each for red, green, and blue. FF = 15×16 + 15 = 255 (full red). 88 = 8×16 + 8 = 136 (some green). 00 = 0 (no blue). Together they make orange.

GDScript
var orange := Color("#FF8800")
print(orange.r8, " ", orange.g8, " ", orange.b8)   # 255 136 0
print(0b1011)   # 11  (binary literal)
print(0xFF)     # 255 (hex literal)

Not every decimal fits

Fractions cause a surprise. In base 10, 1/3 never ends: 0.333… In base 2, even 0.1 never ends. Computers round it to the nearest value they can store, so in many languages, including JavaScript and Python, 0.1 + 0.2 prints 0.30000000000000004. The lesson for programmers: do not test decimals with exact equality. Check whether two values are close instead. GDScript provides is_equal_approx(a, b) for this.

Common mistake

Writing if total == 0.3: after adding decimals. The test can fail even when the math “should” work. Compare with a small tolerance, or count in whole units such as cents instead of dollars.

Where this comes from in school math

Place value and exponents from pre-algebra are the whole story: a binary number is a sum of powers of 2, just as a decimal number is a sum of powers of 10. Scientific notation is also how computers store very large and very small decimals, called floating-point numbers.

Where you will see it

  • Colors: every pixel is a few bytes of red, green, and blue.
  • Settings: one byte can store eight on/off options, one per bit.
  • Files and networks: sizes are counted in bytes, kilobytes, and megabytes.
  • Game engines: Godot’s collision layers are bits in a number.

Exercises

What is binary 110010 in base 10?

32 + 16 + 2 = 50.

Write 100 in binary.

100 = 64 + 32 + 4, so 1100100.

What color is #00FF00? What about #808080?

Pure green. #808080 has 128 in each channel, a medium gray.