Number Systems (Binary, Hex)

Every digital circuit stores and moves information as voltages that are either high or low. We write those two states as 1 and 0, so the natural way to count inside a computer is binary. People, on the other hand, think in decimal, and long strings of 1s and 0s are hard to read. That is why engineers also use hexadecimal as a compact way of writing binary.

In this lesson you will learn how positional number systems work, then convert between decimal, binary and hexadecimal step by step. These skills come back in every later topic: truth tables, adders, memory addressing and Verilog all use them.

Learning objectives

After this lesson you will be able to:

  • Explain positional notation and the meaning of a base (radix).
  • Convert a binary number to decimal using bit weights.
  • Convert a decimal number to binary by repeated division by 2.
  • Convert between binary and hexadecimal by grouping bits in fours.
  • State how many values n bits can represent.

Positional notation and the base

In a positional number system, the value of a digit depends on where it sits. In decimal (base 10) each position is worth ten times the one to its right: 1s, 10s, 100s, 1000s. The number 345 means 3 x 100 + 4 x 10 + 5 x 1.

Binary (base 2) works the same way, but each position is worth two times the one to its right, and only the digits 0 and 1 exist. Hexadecimal (base 16) uses sixteen symbols: the digits 0 to 9 and then the letters A to F for the values ten to fifteen.

Decimal Binary Hex
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Learn this table. Once you know it, binary-to-hex conversion becomes quick and almost automatic.

Bit weights

Each binary position has a weight that is a power of two. For an 8-bit number the weights, from left to right, are:

Bit position 7 6 5 4 3 2 1 0
Weight 128 64 32 16 8 4 2 1

The leftmost bit is the most significant bit (MSB) and the rightmost bit is the least significant bit (LSB). With n bits you can represent 2^n different values, from 0 up to 2^n - 1. So 4 bits give 16 values (0 to 15), and 8 bits give 256 values (0 to 255).

Example 1: binary to decimal

Convert 101101 (binary) to decimal.

Write the weights under the bits, from the right: 1, 2, 4, 8, 16, 32.

Bit 1 0 1 1 0 1
Weight 32 16 8 4 2 1

Add the weights wherever the bit is 1: 32 + 8 + 4 + 1 = 45.

Result: 101101 in binary equals 45 in decimal.

Example 2: decimal to binary to hex

Convert 156 (decimal) to binary and then to hexadecimal.

Step 1: divide by 2 repeatedly and keep the remainders.

Division Quotient Remainder
156 / 2 78 0
78 / 2 39 0
39 / 2 19 1
19 / 2 9 1
9 / 2 4 1
4 / 2 2 0
2 / 2 1 0
1 / 2 0 1

Read the remainders from the bottom up: 10011100.

Step 2: check with weights. 128 + 16 + 8 + 4 = 156.

Step 3: group into fours. 1001 1100. From the table, 1001 is 9 and 1100 is C.

Result: 156 in decimal equals 10011100 in binary equals 9C in hexadecimal.

Example 3: converting between hex and binary

Hex and binary convert digit by digit, because one hex digit is exactly four bits.

Hex to binary. Convert 3F7A (hex).

Hex digit 3 F 7 A
Binary 0011 1111 0111 1010

Result: 3F7A in hex equals 0011111101111010 in binary.

Binary to hex. Convert 110101101 (binary). Start from the right and group the bits in fours, adding zeros on the left if needed: 1 1010 1101 becomes 0001 1010 1101. That reads 1, A, D.

Result: 110101101 in binary equals 1AD in hex.

The most common way to go wrong is to group from the left. Always group from the right, so that the last group lines up with the least significant bit.

Common mistakes

  • Reading remainders in the wrong order. In repeated division the first remainder is the LSB, so the answer is read from the bottom up.
  • Grouping bits from the left. Always start at the right-hand end.
  • Treating hex letters as variables. A to F are just digit values (10 to 15).
  • Forgetting leading zeros. 1 and 0001 are the same value, but hardware has a fixed width, so a 4-bit register always holds four digits.
  • Mixing up 2^n and 2^n - 1. Eight bits give 256 values, but the largest one is 255.

Practice problems

Check your answers with the number base converter.

  1. Convert 11010110 (binary) to decimal.

    Answer: 128 + 64 + 16 + 4 + 2 = 214.

  2. Convert 237 (decimal) to binary.

    Answer: 11101101.

  3. Convert B5E (hex) to binary.

    Answer: 1011 0101 1110.

  4. Convert 101111000011 (binary) to hex.

    Answer: 1011 1100 0011 gives BC3.

Frequently asked questions

Why do computers use binary instead of decimal? A transistor is most reliable as a switch with two clear states, on or off. Telling ten different voltage levels apart would be slower and far more sensitive to noise, so digital hardware sticks to two.

Why is hexadecimal used instead of binary? Hex is a shorthand for binary. One hex digit stands for exactly four bits, so a 32-bit value takes 8 hex digits instead of 32 ones and zeros, and conversion between the two needs no arithmetic.

What about octal? Octal (base 8) groups bits in threes instead of fours. It was popular on older machines and still appears in a few places, such as file permissions, but hex is far more common in modern digital design.

Once you are comfortable converting numbers, the truth table generator is a good way to see binary counting at work: the input columns simply count upward in binary.

Next lesson: Basic Logic Gates: AND, OR, NOT