Binary and Hexadecimal
Binary is the place value you already use for decimal, just with powers of 2 instead of powers of 10. Hexadecimal is shorthand for binary: since , each hex digit stands for exactly four bits, so you can read the bits right off it.
What the symbols mean
- Bit i
The digit in the 2ⁱ place, 0 or 1.
- Number of bits
How many bits the pattern has.
Explanation
Binary is simply a way of writing numbers with only two digits, 0 and 1, and hexadecimal is a way of writing them with sixteen. The names just count digits: bi- means two, and hexadecimal glues Greek hexa (six) to decimal (ten), so sixteen. Binary is the natural language of hardware, where a wire is either on or off, and hex is the shorthand engineers use to read it quickly. The formula looks scary, but it's just three pieces: (each bit, 0 or 1), (what that position is worth: 1, 2, 4, 8, …) and (add them all up). It's the same thing you already do with a number like 407 in decimal; bear with it.
Place value with 2s. You've used place value for years: the decimal numeral 407 means . Binary is the same idea with powers of 2 in place of powers of 10, so each digit, a bit, only needs to be 0 or 1:
Hardware likes base 2 because a wire only has to tell two voltages apart, high and low, which stays reliable even with noise.
Decimal to binary. Divide by 2 and keep the remainders. Think about why that works. Write . Every bit except the last one is worth an even amount, so the remainder has to be the last bit, and is all the other bits shifted down one place. So you just repeat on . Notice the remainders come out last bit first.
How many patterns. Each of bits has 2 choices, so by the product rule there are patterns, standing for through . A byte (8 bits) holds 0 to 255. Going the other way, needs bits. The largest -bit number is the geometric series , which is why 0xFF + 1 = 0x100.
Hexadecimal. Long rows of bits are hard to read, so engineers write them in base 16, with digits 0 to 9 and A to F (A = 10, …, F = 15). Since , each hex digit is exactly four bits, and converting is just regrouping:
Decimal digits don't line up with bits like this, because 10 isn't a power of 2. That's why datasheets give register addresses and bit masks in hex: you can read the bits right off. Code marks the base with a prefix, so 0xB5, 0b10110101 and 181 are the same number in C, Python and JavaScript. Octal (base 8, three bits per digit) survives in Unix file permissions like 0755.
In this unit you can type answers the way code writes them: 0b1011 for binary and 0x2D for hex.
Counting in binary. Count 0, 1, 10, 11, 100, … and keep an eye on each bit: bit 0 flips every step, bit 1 every 2 steps, bit every steps. So each bit of a counter is a square wave at half the frequency of the bit below it. In other words, a binary counter is a frequency divider, and that's how a chip makes slower clocks from a fast one. There's an animation further down that shows a 4-bit counter doing exactly this.
See it
Worked examples
Convert 45 to binary and to hexadecimal.
- 1
Divide by 2 and keep the remainders: , , , , , .
What number is 0xC8, and what are its bits?
- 1
C stands for 12, so 0xC8 .
Practice
Type your answer and check it. Solve them all to complete the lesson.
What is in decimal?
Write 45 in binary. Type it the way C or Python would, with the prefix 0b (like 0b1010).
Write 200 in hexadecimal, with the prefix 0x.
How many 1 bits does 0x3F have?
How many different values can a 12-bit analog-to-digital converter report?
How many bits do you need to store every whole number from 0 to 1000?
A digital watch counts the ticks of a 32,768 Hz quartz crystal in a binary counter, and bit of a counter flips once every ticks. Which bit flips exactly once per second, so the watch can use it to advance the seconds?
The last hex digit of a number is its remainder mod 16. What is the last hex digit of 2026, as a number from 0 to 15?
The hardest one here. Get it, and you've got this lesson.
How many digits does have in hexadecimal, and how many in decimal? Enter both, hex first.
Where it's used
Every register map in a microcontroller datasheet is written in hex, because each hex digit lines up with four bits. And watch crystals run at 32,768 Hz because a 15-bit binary counter divides that down to exactly one tick per second.