Home / Articles / Computer Science Mathematics
Computer Science Mathematics · 6 min read

Binary, Octal and Hexadecimal: Number Systems in Computing

Learn how binary, octal and hexadecimal work, how to convert between bases step by step, and why computers use them for memory, colours and negative numbers.

Every photo, message and game on your phone is ultimately stored as a long string of 0s and 1s. To read and reason about that data, programmers use three closely related number systems: binary (base 2), octal (base 8) and hexadecimal (base 16). Once you understand place value, converting between them becomes a quick, mechanical skill that appears in Matric and FSc computer science, O/A Level, and every first-year university course.

Place Value: The Idea Behind Every Base

In our everyday decimal system, the number 472 means

$$ 472 = 4 \times 10^2 + 7 \times 10^1 + 2 \times 10^0 $$

The base is 10, so we use ten digits (0 to 9) and each position is worth ten times the one to its right. Any whole number \(b \geq 2\) can serve as a base. In base \(b\), the digits run from 0 to \(b-1\) and the positions are worth \(b^0, b^1, b^2, \dots\) from right to left.

  • Binary (base 2): digits 0 and 1. Place values 1, 2, 4, 8, 16, 32, 64, 128, ...
  • Octal (base 8): digits 0 to 7. Place values 1, 8, 64, 512, ...
  • Hexadecimal (base 16): digits 0 to 9 and then A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Place values 1, 16, 256, 4096, ...

To avoid confusion we write the base as a subscript, for example \(1011_2\), \(17_8\) or \(2F_{16}\). In programming languages you will often see prefixes instead: 0b1011, 0o17 and 0x2F.

Why Computers Use Binary

A computer's memory is built from billions of tiny switches (transistors) that are either off or on. Two states map perfectly onto the two binary digits, called bits. Eight bits make a byte, which can hold \(2^8 = 256\) different values, from 0 to 255.

Binary is ideal for hardware but tiring for humans: the number 213 is already eight digits long in binary. Octal and hexadecimal act as shorthand. Because \(8 = 2^3\) and \(16 = 2^4\), each octal digit stands for exactly 3 bits and each hex digit stands for exactly 4 bits. That neat fit is the whole reason these bases are popular.

Converting Between Bases

Any base to decimal

Multiply each digit by its place value and add. For example,

$$ 2F7_{16} = 2 \times 256 + 15 \times 16 + 7 \times 1 = 512 + 240 + 7 = 759 $$

Decimal to any base

Divide repeatedly by the base and record the remainders. The remainders, read from bottom to top, give the digits.

Binary to octal or hex

Group the bits from the right: in threes for octal, in fours for hex. Pad with leading zeros if needed, then convert each group.

Worked Example 1: Converting 213 to Binary, Octal and Hex

Step 1: Divide by 2 repeatedly.

  1. \(213 \div 2 = 106\) remainder 1
  2. \(106 \div 2 = 53\) remainder 0
  3. \(53 \div 2 = 26\) remainder 1
  4. \(26 \div 2 = 13\) remainder 0
  5. \(13 \div 2 = 6\) remainder 1
  6. \(6 \div 2 = 3\) remainder 0
  7. \(3 \div 2 = 1\) remainder 1
  8. \(1 \div 2 = 0\) remainder 1

Reading the remainders from bottom to top gives \(213 = 11010101_2\).

Step 2: Check. The 1s sit in the positions worth 128, 64, 16, 4 and 1:

$$ 128 + 64 + 16 + 4 + 1 = 213 \quad \checkmark $$

Step 3: Convert to octal. Group in threes from the right: 11 010 101. Pad the left group to 011. Now 011 = 3, 010 = 2, 101 = 5, so \(213 = 325_8\). Check: \(3 \times 64 + 2 \times 8 + 5 = 192 + 16 + 5 = 213\).

Step 4: Convert to hex. Group in fours: 1101 0101. Now 1101 = 13 = D and 0101 = 5, so \(213 = D5_{16}\). Check: \(13 \times 16 + 5 = 208 + 5 = 213\).

Notice how much shorter D5 is than 11010101, yet it carries exactly the same information.

Worked Example 2: Binary Fractions

Positions to the right of the binary point are worth \(2^{-1} = 0.5\), \(2^{-2} = 0.25\), \(2^{-3} = 0.125\), and so on. Convert \(10110.101_2\) to decimal.

Whole part: \(10110_2 = 16 + 4 + 2 = 22\).

Fractional part: \(.101_2 = 1 \times 0.5 + 0 \times 0.25 + 1 \times 0.125 = 0.625\).

$$ 10110.101_2 = 22.625 $$

Going the other way, convert a decimal fraction by multiplying by 2 repeatedly and taking the integer parts. For 0.625: \(0.625 \times 2 = 1.25\) (digit 1), \(0.25 \times 2 = 0.5\) (digit 0), \(0.5 \times 2 = 1.0\) (digit 1), stop. So \(0.625 = 0.101_2\). Some fractions, such as 0.1, never terminate in binary, which is why computers cannot store 0.1 exactly.

Worked Example 3: Negative Numbers with Two's Complement

Computers usually store signed integers using two's complement. In an 8-bit system, the leftmost bit has place value \(-128\) instead of \(+128\). To find the pattern for \(-45\):

  1. Write \(+45\) in 8 bits: \(45 = 32 + 8 + 4 + 1\), so \(00101101\).
  2. Invert every bit: \(11010010\).
  3. Add 1: \(11010011\).

Check: reading \(11010011\) with the leftmost bit worth \(-128\):

$$ -128 + 64 + 16 + 2 + 1 = -45 \quad \checkmark $$

The beauty of two's complement is that ordinary binary addition just works. Adding \(00101101\) (+45) and \(11010011\) (\(-45\)) gives \(1\,00000000\); the ninth bit falls off the end of the 8-bit register, leaving 0, exactly as it should.

Where You Meet These Bases in Real Life

  • Web colours: #FF8800 is three hex bytes for red, green and blue: FF = 255, 88 = 136, 00 = 0, giving an orange colour.
  • Memory addresses: debuggers display addresses such as 0x7FFE0040 because each hex digit maps cleanly to 4 bits.
  • MAC addresses: network cards are identified by six hex bytes such as 3C:5A:B4:01:9F:E2.
  • Unix file permissions: chmod 755 is octal. Each digit encodes read (4), write (2) and execute (1), so 7 = rwx and 5 = r-x.
  • Unicode: the Urdu letter alif is U+0627, a hexadecimal code point.

Common Mistakes to Avoid

  • Reading the remainders top to bottom instead of bottom to top.
  • Grouping bits from the left instead of from the right when converting to octal or hex.
  • Forgetting that hex digits A to F represent 10 to 15.
  • Treating the leftmost bit of a two's complement number as positive.

Did You Know?

  • Gottfried Wilhelm Leibniz published an essay on binary arithmetic around 1703, more than two centuries before electronic computers existed.
  • The largest value in one byte, 255, is \(FF_{16}\), which is why colour channels and IP address parts stop at 255.
  • Some early computers, such as the PDP-8, worked naturally in octal because their word length (12 bits) divides evenly into groups of three.

Key Takeaways

  • In base \(b\), each position is worth a power of \(b\); convert to decimal by multiplying and adding.
  • Convert decimal to another base by repeated division, reading remainders from bottom to top.
  • One octal digit equals 3 bits and one hex digit equals 4 bits, so grouping bits makes conversion instant.
  • Two's complement stores negative integers so that ordinary addition still gives correct results.
  • Some decimal fractions, like 0.1, have no exact binary representation.

Stuck on a number systems assignment or a tricky conversion? Send your question to Amathify's subject experts and PhD scholars via WhatsApp or the Ask a Question form for clear, step-by-step solutions.

Stuck on a problem or assignment?

Our experts and PhD scholars solve maths, physics and chemistry — step by step.

WhatsApp usAsk a question

Related articles