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.
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
0–9 — 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}.