Binary, Hexadecimal & Decimal lessons
Bits that count
Eight bit switches with place values 128 to 1, adding up live in decimal, hex and a shade of red.
A computer stores every number as a row of switches called bits. Each bit is either off (0) or on (1).
Every switch has a place value. Add up the place values of the switches that are on, and you get the number.
The same number can be written three ways: decimal (base 10), binary (base 2) and hexadecimal (base 16).
Place value
In decimal, each place is worth ten times the place to its right. The digits run from 0 to 9.
347 = 3 \times 100 + 4 \times 10 + 7 \times 1= 300 + 40 + 7(hundreds, tens and ones)
Binary works the same way, but each place is worth twice the place to its right. Its only digits are 0 and 1.
Place values built right to left by multiplying by the base, for base 10 and base 2, then a number expanded over them.
Binary to decimal
Write the place values over the bits. Start with 1 on the right and double each time you move left.
\textcolor{#f871b9}{1}\ 0\ \textcolor{#f871b9}{1}\ \textcolor{#f871b9}{1}_2(place values 8, 4, 2, 1)\textcolor{#f871b9}{1} \times 8 + 0 \times 4 + \textcolor{#f871b9}{1} \times 2 + \textcolor{#f871b9}{1} \times 18 + 2 + 1 = \textcolor{#3b82f6}{11}(add the places that hold a 1)
Add the place values that sit over a 1, and skip the ones over a 0.
A small 2 after a number, as in 1011_2, marks it as binary. Without it, you might read it as one thousand and eleven.
Practice: Binary to decimal
Type the decimal value of a random 4 to 8 bit number.
Counting in binary
Counting in binary uses the same carry rule as decimal. When a place goes past its biggest digit, it rolls back to 0 and carries 1 to the left.
Decimal rolls over after 9. Binary rolls over after 1, so carries happen much sooner.
A decimal counter and a 4 bit binary counter counting together, with every roll over and carry lit up.
Bits, nibbles and bytes
A bit is one binary digit. A group of 4 bits is a nibble, and a group of 8 bits is a byte.
Each extra bit doubles the number of patterns. With n bits there are 2^n patterns.
Adding one bit at a time: the old list is copied once with a 0 in front and once with a 1, so the count doubles.
A byte has 2^8 = 256 patterns. So it can hold any whole number from 0 to 255.
Practice: How many bits?
Sort numbers by the smallest group of bits that can hold them: a nibble, a byte, or more.
Decimal to binary
Work down the place values from the biggest. If a place value fits into what is left, write 1 and subtract it.
If it does not fit, write 0 and move on. When nothing is left, every remaining bit is 0.
A decimal number turned into binary place by place: fits, write 1 and subtract; does not fit, write 0.
Practice: Decimal to binary
Type a random decimal number from 4 to 255 in binary.
Hexadecimal
Hexadecimal, or hex, is base 16. It needs sixteen digits, so after 0 to 9 it uses the letters A to F.
A is 10, B is 11, and so on up to F, which is 15. One hex digit covers exactly the 16 patterns of a nibble.
The values 0 to 15 in decimal, binary and hex, stepping through the letter digits.
Hex to decimal
In hex, each place is worth 16 times the place to its right. A two digit hex number has a 16s place and a 1s place.
\textcolor{#8b5cf6}{2A}_{16}(A is 10)\textcolor{#8b5cf6}{2} \times 16 + \textcolor{#8b5cf6}{10} \times 132 + 10 = \textcolor{#3b82f6}{42}
Programmers often write 0x in front of hex, as in 0x2A. It means the same as a small 16 after the number.
Practice: Hex to decimal
Type the decimal value of a random two digit hex number.
Decimal to hex
To turn a number from 0 to 255 into hex, count how many whole 16s fit into it. That count is the first digit, and what is left over is the second.
\textcolor{#3b82f6}{200} = 12 \times 16 + 8(12 whole 16s, 8 left over)12 \to \textcolor{#8b5cf6}{C},\ \ 8 \to \textcolor{#8b5cf6}{8}\textcolor{#3b82f6}{200} = \textcolor{#8b5cf6}{C8}_{16}
A 16 by 16 grid filled with the number: full rows give the first hex digit, the leftover cells give the second.
Practice: Decimal to hex
Type a random decimal number from 10 to 255 in hex.
Binary and hex by nibbles
Since 16 = 2^4, one hex digit always matches one nibble. So you can swap between binary and hex without going through decimal.
Split the bits into groups of 4 from the right. Then turn each group into one hex digit.
\textcolor{#f871b9}{1101}\ \textcolor{#f871b9}{0110}_2(two nibbles)8 + 4 + 1 = 13,\ \ 4 + 2 = 6\textcolor{#8b5cf6}{D6}_{16}(13 is D)
A byte splits into two nibbles, each nibble adds up its 8, 4, 2, 1 place values and becomes one hex digit.
Practice: Match nibbles to hex
Drag each hex digit onto its nibble. Every round pairs nibbles that are mirror images, like B and D.
Binary addition
Add binary numbers in columns from right to left, just like decimal. There are only four sums to know.
0 + 0 = 0,\ \ 0 + 1 = 11 + 1 = 10_2(write 0, carry 1)1 + 1 + \textcolor{#f871b9}{1} = 11_2(with a carry: write 1, carry 1)
Column addition of two binary numbers, played right to left with each carry moving up to the next column.
Practice: Add binary numbers
Add two random binary numbers and type the sum in binary.
Overflow
Computers store numbers in a fixed number of bits. A 4 bit register can only hold 0 to 15.
If a sum needs one more bit, the last carry has nowhere to go. It is lost, and this is called overflow.
Sums added into a 4 bit register: when a fifth bit is needed, the carry drops off and the register keeps the wrong answer.
A byte overflows the same way. For 200 + 100, it keeps only 44, which is 300 minus 256.
Practice: Spot the overflow
Pick what a 4 bit register holds after a sum, including sums that overflow.
Hex colour codes
Screens make every colour from red, green and blue light. Each of the three channels is one byte, from 0 to 255.
A colour code like #FF8800 is three hex pairs: red FF, green 88 and blue 00. That is full red, about half green and no blue, which looks orange.
Red, green and blue sliders from 0 to 255, with the colour, its hex code and each channel in binary.
Practice: Read a colour code
Pick the swatch for a colour code, or read how much of one channel a code holds.
Where each base is used
Decimal is for people: prices, ages and scores. Binary is what the hardware stores, as bits in memory and on the wire.
Hex is for people reading binary. Colour codes, memory addresses and error codes use it, because two hex digits show a whole byte.
Sort real examples by the base you would see them written in.
Practice: Mixed conversions
One value given in decimal, binary or hex; type it in the other two bases.