Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 67 ยท Data Representation

Number Systems and Conversions

Learn positional number systems, how to convert between binary, octal, decimal and hexadecimal by hand and in Python, and why hardware uses these bases.

A processor stores everything as patterns of two states, so its natural way to write a number is binary. People, however, read decimal. In between sit octal and hexadecimal, compact notations that let engineers read long binary patterns without losing their minds. Converting between these systems is a basic skill in computer organization: you will use it for memory addresses, instruction encodings, colors, masks and more.

This lesson explains positional notation, gives you step-by-step conversion algorithms, and implements them in Python so you can check your hand work. For how bits and bytes themselves are defined, see Bits and Bytes; for the operators that manipulate bits in code, see Bitwise Operators.

Positional notation

In a positional number system each digit has a weight that depends on its position. In base b, the digits run from 0 to b - 1, and the weight of position k (counting from 0 on the right) is b^k. The value of a number is the sum of digit x weight.

The decimal number 4,253 means 4 x 10^3 + 2 x 10^2 + 5 x 10^1 + 3 x 10^0. Binary works the same way with base 2:

Position 7 6 5 4 3 2 1 0
Weight 128 64 32 16 8 4 2 1
Bits of 1011 0110 1 0 1 1 0 1 1 0

So 1011 0110 = 128 + 32 + 16 + 4 + 2 = 182. The rightmost bit is the least significant bit (LSB) and the leftmost is the most significant bit (MSB).

The four bases you will meet most often:

Base Name Digits Prefix in many languages
2 Binary 0, 1 0b
8 Octal 0 to 7 0o (or a leading 0)
10 Decimal 0 to 9 none
16 Hexadecimal 0 to 9, A to F 0x

In hexadecimal, A to F stand for 10 to 15. Case does not matter: 0xff equals 0xFF.

Binary to decimal and back

Binary to decimal: add up the weights of the positions that hold a 1 (as above).

Decimal to binary has two classic methods.

  1. Repeated division by 2. Divide the number by 2, write down the remainder, and repeat with the quotient until it is 0. The remainders read from last to first give the binary number. For 182: 182 / 2 = 91 r 0, 91 / 2 = 45 r 1, 45 / 2 = 22 r 1, 22 / 2 = 11 r 0, 11 / 2 = 5 r 1, 5 / 2 = 2 r 1, 2 / 2 = 1 r 0, 1 / 2 = 0 r 1. Reading upward: 10110110.
  2. Subtract powers of two. Find the largest weight that fits, write a 1, subtract, and move on.

The same division method works for any base: divide by 8 for octal, by 16 for hex.

Python Convert any base with repeated division

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 loop mirrors the hand method exactly. Each iteration peels off the least significant digit, which is why the digits come out in reverse order and the list is reversed at the end.

Converting from any base to decimal

To read a string of digits in base b, use Horner's method: start with 0 and for each digit compute value = value * b + digit. It avoids computing powers.

Python From any base to decimal (Horner's method)

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 built-in int(text, base) does the same job, and bin(), oct(), hex() and format(n, "b") go the other way. Writing your own version once is a good way to make sure you understand what they do.

The shortcut: binary, octal and hex

Octal and hex exist because 8 and 16 are powers of 2. One octal digit stands for exactly 3 bits and one hex digit for exactly 4 bits. Conversion between binary and these bases needs no arithmetic: just regroup the bits.

Binary to hex: split the bits into groups of 4 starting at the right, then replace each group by one hex digit.

Text
1011 0110  ->  B 6   ->  0xB6

Hex to binary: replace each hex digit by its 4 bits.

Binary to octal: group by 3 bits. 10110110 becomes 010 110 110 (pad with a zero on the left) = 2 6 6, so 0o266.

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
flowchart LR
  A["Binary<br/>1011 0110"] -->|"group by 4"| B["Hex<br/>B 6"]
  A -->|"group by 3"| C["Octal<br/>2 6 6"]
  B -->|"expand each digit"| A
  A -->|"add up weights"| D["Decimal<br/>182"]

Binary regroups directly into hex or octal; decimal needs arithmetic.

Hexadecimal is the favourite of the three. One byte is exactly two hex digits (0x00 to 0xFF), so memory dumps, machine code and color codes like #FF8800 are all written in it. Octal survives mostly in Unix file permissions, where one octal digit encodes the three read/write/execute bits of one class of user.

Python Regrouping bits without arithmetic

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 last lines show why octal lasts in file permissions: rwxr-x--- splits into rwx, r-x and ---, which are 7, 5 and 0, so the familiar mode is 750.

Fractions in other bases

Positions to the right of the point carry negative powers: 2^-1 = 0.5, 2^-2 = 0.25, 2^-3 = 0.125. To convert a decimal fraction to binary, multiply by 2 repeatedly and record the integer part each time. For 0.625: 0.625 x 2 = 1.25 (digit 1), 0.25 x 2 = 0.5 (digit 0), 0.5 x 2 = 1.0 (digit 1). So 0.625 = 0.101 in binary.

Some fractions never terminate. 0.1 in decimal is 0.0001100110011... in binary, repeating forever, just as 1/3 repeats in decimal. That is the root cause of the rounding surprises in Floating-Point Numbers and IEEE 754.

Python Fraction to binary by repeated doubling

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 0.625 and 0.75 conversions end exactly (leftover 0). For 0.1 the leftover never reaches zero, so the digits would go on forever.

To estimate sizes quickly, remember that 2^10 is about a thousand, 2^20 about a million and 2^30 about a billion. With this, 2^26 is 2^6 x 2^20, about 64 million.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the decimal value of the binary number 1101?

    8 + 4 + 0 + 1 = 13.
  2. 2. Which hexadecimal number equals binary 1111 0000?

    Group by 4: 1111 is F and 0000 is 0, so the value is 0xF0 (240 decimal).
  3. 3. Why is hexadecimal so convenient for binary data?

    Because 16 = 2^4, each hex digit is a 4-bit group, so conversion is just regrouping.
  4. 4. Which method converts the decimal integer 45 to binary?

    Dividing by 2 and reading the remainders in reverse gives 101101, which is 45.
  5. 5. Why can't 0.1 be written exactly with a finite number of binary digits?

    0.1 = 0.0001100110011... in binary: the pattern 0011 repeats without end, like 1/3 in decimal.

Summary

  • In base b each position has weight b^k; the value is the sum of digit x weight.
  • Convert to base b by repeated division (remainders in reverse); convert from base b with Horner's method.
  • Octal groups 3 bits and hex groups 4 bits, so binary conversion is regrouping without arithmetic.
  • One byte is exactly two hex digits; octal survives in Unix permissions.
  • Fractions convert by repeated doubling; many decimal fractions repeat forever in binary.

Continue with Signed Integers and Two's Complement.