Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 57 ยท Digital Foundations

Binary, Octal and Hexadecimal

Learn positional notation and how to convert between binary, octal, decimal and hexadecimal by hand and in Python, plus bits, nibbles and bytes.

People count in tens, probably because we have ten fingers. A digital circuit has only two states per wire, so it counts in twos. Engineers also use two shorthand systems, octal and hexadecimal, that are easy to translate to and from binary and much shorter to write. If you can move comfortably between these systems, reading memory dumps, color codes, addresses and truth tables becomes easy.

This lesson explains the idea behind every number system, shows how to convert in both directions with a worked method, and then lets you check your answers with Python.

Positional notation

In decimal, the digits of 3,052 mean three thousands, zero hundreds, five tens and two ones. Each position is worth ten times the position to its right:

3052 = 3 ร— 10ยณ + 0 ร— 10ยฒ + 5 ร— 10ยน + 2 ร— 10โฐ

A system with base (or radix) r uses r different digit symbols, from 0 to r โˆ’ 1, and each position is worth r times the one to its right. Only the base changes:

System Base Digits Example
Binary 2 0, 1 1011
Octal 8 0 to 7 13
Decimal 10 0 to 9 11
Hexadecimal 16 0 to 9, A to F B

All four examples in the last column are the same quantity, eleven. In hexadecimal the letters A to F stand for the values 10 to 15, since a single symbol is needed for each digit value. The rightmost bit of a binary number is the least significant bit (LSB) and the leftmost is the most significant bit (MSB).

Same quantity, four spellings

Binary Base 2, digits 0 and 1.
Octal Base 8, one digit per 3 bits.
Decimal Base 10, what people use.
Hex Base 16, one digit per 4 bits.

Eleven is 1011 in binary, 13 in octal, 11 in decimal and B in hex.

1 / 2

Binary to decimal

To convert any number to decimal, multiply each digit by its place value and add. For binary, the place values are the powers of two: 1, 2, 4, 8, 16, 32, 64, 128 and so on.

Take 101101โ‚‚. From the right, the positions are 0 to 5:

1ร—32 + 0ร—16 + 1ร—8 + 1ร—4 + 0ร—2 + 1ร—1 = 32 + 8 + 4 + 1 = 45

A shortcut is to memorize the first powers of two and simply add up the places that hold a 1.

Decimal to binary

The standard method is repeated division by 2. Divide the number by 2, write down the remainder, and continue with the quotient until it reaches 0. Read the remainders from bottom to top.

For 45:

Division Quotient Remainder
45 รท 2 22 1
22 รท 2 11 0
11 รท 2 5 1
5 รท 2 2 1
2 รท 2 1 0
1 รท 2 0 1

Reading the remainders from the last row up gives 101101. The same method works for any base: divide by 8 for octal or by 16 for hexadecimal.

The first example turns both procedures into code. It is deliberately written with loops, not built-in functions, so you can see the algorithm, and then it compares the results with Python's own bin, oct and hex.

Python Convert by hand, then check with Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python's built-ins return text with a prefix: 0b for binary, 0o for octal and 0x for hexadecimal. The same prefixes work in source code, so 0b101101 and 0x2D are both just the integer 45.

The shortcut: binary, octal and hex

Binary is easy for hardware and tiresome for people: the value 1,000,000 is 11110100001001000000โ‚‚. Because 8 = 2ยณ and 16 = 2โด, there is a direct bridge between binary and the other two systems: one octal digit stands for exactly 3 bits and one hex digit for exactly 4 bits.

To convert binary to hex, split the bits into groups of four starting from the right (pad on the left with zeros), then translate each group:

TEXT
1011 0110 1111   binary, grouped by 4
 B    6    F     hexadecimal

So 101101101111โ‚‚ = B6Fโ‚โ‚†. To go back, replace each hex digit with its four bits. For octal the groups are three bits wide:

TEXT
101 101 101 111   binary, grouped by 3
 5   5   5   7    octal

The hex digit table is worth memorizing, since you will see it again and again:

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

Bits, nibbles and bytes

Hardware groups bits into fixed-size units:

  • A bit is one binary digit.
  • A nibble is 4 bits, which is exactly one hex digit (0 to 15).
  • A byte is 8 bits, two hex digits or 0 to 255.
  • A word is whatever width a particular processor handles at once, such as 16, 32 or 64 bits.

The largest unsigned value in n bits is 2โฟ โˆ’ 1, so one byte tops out at 255 (hex FF) and 16 bits at 65,535 (hex FFFF). Be careful: in the context of memory sizes, the prefixes kilo, mega and giga may mean powers of 1,000 or of 1,024, depending on who is speaking; the unambiguous names kibibyte (KiB, 1,024 bytes) and mebibyte (MiB) exist for the second meaning.

Hexadecimal dominates in practice. Web colors such as #FF8800 are three bytes (red, green, blue) in hex, and a memory address is almost always shown in hex because it lines up with bytes.

Python Bits, nibbles and bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The f"{value:X}" format letters are worth remembering: b for binary, o for octal, x or X for lower or upper-case hex, and 0 plus a width, as in 08b, pads with zeros so a byte always shows all eight bits. The shifts and masks (>> 4 and & 0x0F) are bitwise operators, which the Python lesson on bitwise operators covers in detail. Later in this course the same operations appear as hardware: shifting is a wiring change, and masking is an AND gate per bit.

Fractions and leading zeros

Positional notation continues to the right of the point with negative powers: 0.101โ‚‚ = 1/2 + 0/4 + 1/8 = 0.625. Many decimal fractions, such as 0.1, have no finite binary expansion, which is why floating-point numbers sometimes surprise you. We will stay with whole numbers in this course.

Leading zeros do not change a value (0011โ‚‚ = 11โ‚‚ = 3), but in hardware the width matters: a 4-bit register always holds exactly four bits. Many tools therefore show values padded to a fixed width.

Common mistakes

  • Reading the remainders in the wrong order. The first remainder is the LSB, so the answer is read bottom to top.
  • Grouping bits from the left. Always group from the right (the point), padding with leading zeros.
  • Leaving out the base. A number without a prefix or subscript is ambiguous: 10 might mean two, eight, ten or sixteen. Write 0x, 0b or a subscript when it matters.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is 101101 in binary as a decimal number?

    32 + 8 + 4 + 1 = 45.
  2. 2. How many bits does one hexadecimal digit represent?

    16 = 2^4, so one hex digit corresponds to exactly four bits (a nibble).
  3. 3. What is 1110 0101 in hexadecimal?

    1110 is E and 0101 is 5, so the byte is E5.
  4. 4. What is the largest unsigned value that fits in one byte?

    Eight bits give 2^8 = 256 combinations, from 0 to 255 (hex FF).
  5. 5. To convert decimal 45 to binary by repeated division, how do you read the remainders?

    The first remainder is the least significant bit, so the last remainder is the most significant and comes first.

Summary

  • A number system with base r gives each position the weight of a power of r; the digits run from 0 to r โˆ’ 1.
  • To convert to decimal, multiply each digit by its place value and add; to convert from decimal, divide repeatedly by the base and read the remainders backwards.
  • One octal digit equals 3 bits and one hex digit equals 4 bits, so conversion between binary and these systems is just regrouping.
  • A nibble is 4 bits, a byte is 8 bits (0 to 255, hex 00 to FF), and n bits hold values up to 2โฟ โˆ’ 1.
  • Python has bin, oct, hex, the 0b, 0o, 0x prefixes and format codes b, o, X for all of this.

Next: Signed Numbers and Two's Complement, how hardware stores negative numbers.