Number Bases: Binary, Octal and Hexadecimal

A computer stores everything (numbers, text, pictures, programs) as patterns of bits. To read and write those patterns you need to count in base 2 (binary). Programmers also use base 16 (hexadecimal) and sometimes base 8 (octal), because they are short ways of writing long binary numbers. In this lesson you will learn to convert between all four bases by hand, and see how C prints and reads numbers in each base.

What you will learn

  • The units of data: bit, nibble, byte and word.
  • How place value works in base 10, base 2, base 8 and base 16.
  • How to convert binary to decimal, and decimal to binary (two methods).
  • How to convert between binary, hexadecimal and octal by grouping bits.
  • How to convert decimal to hexadecimal and back.
  • How C writes numbers in other bases with 0x, %x and %o, and one octal trap to avoid.

Part 1: Bits, nibbles, bytes and words

UnitSizeNotes
Bit1 binary digitShort for binary digit. Holds 0 or 1.
Nibble4 bitsOne hexadecimal digit. 16 possible patterns.
Byte8 bitsThe smallest unit most memory can address. 256 patterns, so one byte can hold an unsigned number from 0 to 255.
WordDepends on the processorThe number of bits the processor handles in one go, usually 32 or 64 today. See Memory Types and Features.

With n bits there are 2n different patterns. So 4 bits give 16 patterns and 8 bits give 256. This rule comes up again in every data representation lesson.

Part 2: Place value in any base

In base 10, each column is worth 10 times the column to its right: units, tens, hundreds. In base b, each column is worth b times the column to its right, and the digits go from 0 up to b − 1.

BaseNameDigitsColumn values (right to left)
2Binary0, 11, 2, 4, 8, 16, 32, 64, 128, …
8Octal0 to 71, 8, 64, 512, …
10Decimal0 to 91, 10, 100, 1000, …
16Hexadecimal (hex)0 to 9, then A to F1, 16, 256, 4096, …

Hex needs six extra digits for the values 10 to 15, so it uses letters: A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15. When a number could be read in more than one base, write the base as a small subscript, for example 1012 = 510, or 1F16 = 3110.

DecimalBinary (4 bits)HexOctal
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000810
91001911
101010A12
111011B13
121100C14
131101D15
141110E16
151111F17

Part 3: Binary and decimal

Binary to decimal

Write the place values above the bits and add up the place values of the columns that hold a 1.

Worked example 1. Convert 1011 0110 to decimal.

Place value1286432168421
Bit10110110

1011 0110 = 128 + 32 + 16 + 4 + 2 = 182.

Decimal to binary: method 1, divide by 2

Divide by 2 again and again, writing down each remainder, until the quotient is 0. Then read the remainders from the bottom up. The first remainder is the rightmost bit (the units).

Worked example 2. Convert 77 to binary.

DivideQuotientRemainder
77 ÷ 2381
38 ÷ 2190
19 ÷ 291
9 ÷ 241
4 ÷ 220
2 ÷ 210
1 ÷ 201

Reading the remainders from the bottom up gives 1001101. As an 8-bit byte that is 0100 1101. Check: 64 + 8 + 4 + 1 = 77. ✔

Decimal to binary: method 2, subtract place values

Start at the largest place value that fits and work down. Write 1 if you can subtract it, 0 if you cannot. For 77: 77 − 64 = 13 (1 in the 64s), 32 no, 16 no, 13 − 8 = 5 (1), 5 − 4 = 1 (1), 2 no, 1 − 1 = 0 (1). The 128 column is 0. Result: 0100 1101, the same as before. You may find this faster for 8-bit numbers. See also Decimal to binary.

Try it 1Byte builder

Tap a bit to flip it. The decimal value, and the hex and octal groupings, update straight away. The first row is the high nibble (128 to 16); the second row is the low nibble (8 to 1).

Part 4: Hexadecimal and octal

16 = 24, so one hex digit stands for exactly 4 bits. 8 = 23, so one octal digit stands for exactly 3 bits. That is why converting between these bases and binary needs no arithmetic at all: you just group the bits.

Binary to hex, and hex to binary

Split the binary number into groups of 4 from the right (add 0s on the left if the last group is short) and replace each group with its hex digit. To go back, write each hex digit as 4 bits.

Binary   1011 0110        0010 1111
Hex         B    6           2    F
Answer      B6                2F

Binary to octal, and octal to binary

The same idea with groups of 3. For 1011 0110: regroup from the right as 010 110 110, which gives the octal digits 2, 6, 6. So 1011 0110 = 2668. Check: 2 × 64 + 6 × 8 + 6 = 182, as in Worked example 1. ✔

Hex and decimal

Hex to decimal: multiply each digit by its place value. 2F = 2 × 16 + 15 = 47. B6 = 11 × 16 + 6 = 182.

Decimal to hex: divide by 16 and read the remainders from the bottom up, just like dividing by 2 for binary. 200 ÷ 16 = 12 remainder 8, and 12 ÷ 16 = 0 remainder 12 (C). So 200 = C8. Another route is to go through binary: 200 = 1100 1000 = C8.

Why programmers use hex

  • It is short: a byte is always exactly two hex digits (00 to FF), and a 32-bit address is eight.
  • It is easy to turn back into bits, unlike decimal.
  • You will see it in memory addresses, colour codes on web pages (#FF8800 is one byte each for red, green and blue), network (MAC) addresses and error codes.

Try it 2Repeated division stepper

Type a decimal number, choose the base you want, then tap Next step. Each row divides by the base; the answer is the remainders read from the bottom up.

Part 5: Number bases in C

C lets you write a whole number in three bases in your source code, and print it in three bases with printf:

In codeMeaningprintfPrints
109decimal%d109
0x6Dhex (starts with 0x)%x or %X6d or 6D
0155octal (starts with 0)%o155

All three literals below are the same number, 109. The computer stores the same bits whichever way you write it: the base only changes how we humans read it.

#include <stdio.h>

int main(void)
{
    int a = 0x6D;   /* hex: starts 0x  */
    int b = 0155;   /* octal: starts 0 */
    int c = 109;    /* decimal         */
    int d = 010;    /* a trap: octal!  */

    printf("%d %d %d\n", a, b, c);
    printf("010 is really %d\n", d);
    printf("%x %X %#x %o\n", c, c, c, c);
    return 0;
}

Output:

109 109 109
010 is really 8
6d 6D 0x6d 155

The octal trap. A leading 0 tells C that the number is octal, so 010 is 8, not 10. Never pad decimal numbers with zeros in C code (for example, to line up a list of dates). The # in %#x asks printf to add the 0x prefix.

C has no printf code for binary, so this program uses the divide-by-2 method from Part 3. The remainders are stored in an array (see Arrays in C) and printed backwards, which is the "read from the bottom up" step.

#include <stdio.h>

/* Print n in base 2, using the
   divide-by-2 method from Part 3. */
void print_binary(int n)
{
    int bits[16];
    int count = 0;

    if (n == 0) {
        printf("0");
        return;
    }
    while (n > 0) {
        bits[count] = n % 2; /* remainder */
        n = n / 2;           /* quotient */
        count++;
    }
    /* read the remainders bottom up */
    while (count > 0) {
        count--;
        printf("%d", bits[count]);
    }
}

int main(void)
{
    int nums[4] = {109, 255, 4096, 7};
    int i;

    for (i = 0; i < 4; i++) {
        printf("%d: octal %o, hex %X\n",
               nums[i], nums[i], nums[i]);
        printf("  binary ");
        print_binary(nums[i]);
        printf("\n");
    }
    return 0;
}

Output:

109: octal 155, hex 6D
  binary 1101101
255: octal 377, hex FF
  binary 11111111
4096: octal 10000, hex 1000
  binary 1000000000000
7: octal 7, hex 7
  binary 111

Notice that 4096 is 1000 in hex: 4096 = 163, just as 1000 = 103 in decimal. Try this: change the divide-by-2 function so it takes the base as a second parameter and prints in base 2, 8 or 16. (Hint: print remainders above 9 as letters with printf("%c", 'A' + r - 10).)

Try it 3Conversion drill

Choose a type of conversion. Spaces and leading zeros in your answer are ignored, and hex letters can be capitals or small.

Summary

IdeaKey points
UnitsBit = 1 binary digit; nibble = 4 bits; byte = 8 bits; word = the processor’s natural size. n bits give 2n patterns.
Place valueIn base b each column is worth b times the one to its right. Binary: 1, 2, 4, 8, ... Hex: 1, 16, 256, ...
To decimalMultiply each digit by its place value and add.
From decimalDivide by the base repeatedly; read the remainders from the bottom up. (Or subtract place values for binary.)
Hex ↔ binaryOne hex digit = 4 bits. Group from the right.
Octal ↔ binaryOne octal digit = 3 bits. Group from the right.
In C0x prefix = hex, leading 0 = octal. Print with %x, %X, %o.

Practice questions

1. Convert 1110 0101 to decimal.

Show answer

128 + 64 + 32 + 4 + 1 = 229.

2. Convert 150 to 8-bit binary using repeated division by 2.

Show answer

Remainders (top to bottom): 0, 1, 1, 0, 1, 0, 0, 1. Read upwards: 1001 0110. Check: 128 + 16 + 4 + 2 = 150.

3. Convert 1101 1010 to hexadecimal and to octal.

Show answer

Hex: 1101 = D, 1010 = A, so DA. Octal: 011 011 010 = 332. Both equal 218 in decimal.

4. Convert the hex number 3C to binary and to decimal.

Show answer

3 = 0011, C = 1100, so 0011 1100. Decimal: 3 × 16 + 12 = 60.

5. How many different patterns can be stored in 12 bits? What is the largest unsigned number?

Show answer

212 = 4096 patterns, so the largest unsigned number is 4095 (FFF in hex).

6. Why is it easier to convert binary to hex than binary to decimal?

Show answer

Because 16 is a power of 2 (24), each group of 4 bits maps to exactly one hex digit, so you can convert group by group with no arithmetic. 10 is not a power of 2, so decimal needs place-value sums.

7. What does printf("%d", 017 + 0x11); print?

Show answer

017 is octal = 15, and 0x11 is hex = 17. 15 + 17 = 32, so it prints 32.

Homework

  1. Write your age, your house number and today’s date (as three numbers) in binary, octal and hex. Check each one with Try it 1 or Try it 2.
  2. Find three hex colour codes on a website you use (right-click, Inspect). Split each into its red, green and blue bytes and convert each byte to decimal.
  3. Extend the bases.c program so it reads a number with scanf and prints it in all four bases. Test it with 0, 1, 255, 256 and 65535.
  4. Work through the Data Representation Homework Exercise on this site.

Exam tips

2009 Unit 1 Paper 02, Question 1. Includes a short binary-to-decimal conversion as part of a longer question on gates and binary arithmetic. Show the place values above the bits so the examiner can see your method. Worked solution: 2009 U1 Q1.

Specimen Unit 1 Paper 01 (2021), Items 1 and 2, and 2024 Unit 1 Paper 02, Question 2. These move on to signed numbers, but every one of them starts by converting between decimal and binary, so this lesson’s methods must be automatic. See 2024 U1 Q2.

Hex and octal. Number bases are named in Specific Objective 4. In the papers I have checked (the specimen papers and the solutions on this site) the questions use binary and decimal, but a hex or octal conversion is fair game, so practise with Try it 3.

General advice: write the place values first; give binary answers with the number of bits the question asks for (keep leading zeros); group binary in fours; and always check by converting back. More worked answers are on the CS Solutions page.

Previous: Logic Gates and Truth Tables. Next: Adding Binary Numbers: Carries, Adders and Overflow (lesson 07). See also: Decimal to binary, Binary Manipulation and int vs float: Truncation, Binary, and Choosing the Right Specifier.

Syllabus link: CAPE Computer Science (CXC A19/U2/21), Unit 1, Module 1: Computer Architecture. Specific Objective 4: explain how data is represented in the computer (bits; bytes; number bases).

Video search terms

Search YouTube for: "binary to decimal conversion", "decimal to binary repeated division", "binary to hexadecimal grouping", "hexadecimal explained for beginners".

Try it 4Quick quiz

Tap an answer. All eight questions are self-checks on this lesson.

Updates

2026/11/23

- Removed the Print this lesson button.

- Reworded some sentences so they speak directly to students.

- Published.

2026/11/11

- Draft written (local file, not yet on the site).

© 2026 Vedesh Kungebeharry. All rights reserved.

Leave a Reply