Binary Numbers

Deep inside every computer are billions of tiny switches, and each one can only be on or off. That's it — two states. So if a computer wants to store a number, it can't use ten digits (0 to 9) like we do. It has just two symbols: 0 (off) and 1 (on). Counting with only two symbols is called binary, and each 0 or 1 is called a bit (short for binary digit).

The trick is exactly the same idea as place value in our normal numbers — just with a different "base". In our everyday base-ten, each column is worth ten times the one to its right: ones, tens, hundreds. In binary, each column is worth twice the one to its right: ones, twos, fours, eights, and so on.

Reading a binary number

To turn a binary number into an ordinary (denary) number, write the place values above the bits, then add up every place value that has a 1 beneath it. Step through the example 01101:

So 01101 in binary is 13 in denary.

Try it yourself — change the bits and Run

This little program reads a binary string bit by bit. Each time it sees a new bit, it doubles the running total and adds the bit — which is just another way of doing the place-value sum. Edit the bits string (use only 0s and 1s) and press Run to convert any binary number you like.

// Change these bits (each must be 0 or 1), then press Run! const bits: string = "01101"; let value: number = 0; for (const bit of bits) { value = value * 2 + Number(bit); } console.log(bits + " in binary = " + value + " in denary");

Going the other way: denary to binary

To turn an ordinary number into binary, keep dividing by 2 and write down the remainders (0 or 1) from the bottom up. This program does it for you — change n and Run:

let n: number = 13; let binary: string = ""; while (n > 0) { binary = (n % 2) + binary; // stick the remainder on the front n = Math.floor(n / 2); // halve, throwing away the remainder } console.log("13 in binary is " + binary);

It would be lovely if a wire could carry ten different voltages for the digits 09 — but in the real world, voltages wobble with heat and interference, and telling ten levels apart reliably is hard. Telling apart just two states — "clearly on" vs "clearly off" — is easy and almost never goes wrong. Binary trades a longer number for rock-solid reliability, and that trade is why every digital device on Earth speaks it.

Binary 10 is not ten! Read it with place values: a 1 in the "twos" column and a 0 in the "ones" column, so binary 10 is just two. When it's not obvious from context, people say "one-zero" rather than "ten", and sometimes write the base as a little label: 10_2 = 2_{10}.