People count in tens because they have ten fingers, but a digital machine has only two states. For this reason a computer works in binary, and people who read machine data use octal and hexadecimal as a shorter form of binary.

Positional Number Systems

In a positional number system the value of a digit depends on its position. The base or radix is the number of distinct digits. Table 1.7 lists the four systems used in computing.

Table 1.7: Number systems used in computing

System Base Digits Example
Decimal 10 0 to 9 156
Binary 2 0, 1 10011100
Octal 8 0 to 7 234
Hexadecimal 16 0 to 9, A to F 9C

Definition: Radix. The number of different digits used in a positional number system. The value of a digit is its face value multiplied by the radix raised to the power of its position.

Conversion to Decimal

To change a number to decimal, multiply each digit by its place value and add. For binary 1101 the value is 1×8 + 1×4 + 0×2 + 1×1 = 13. For octal 345 the value is 3×64 + 4×8 + 5 = 229. For hexadecimal 2F the value is 2×16 + 15 = 47.

Conversion from Decimal

To change a decimal integer to another base, divide repeatedly by the base and note the remainders. The remainders read from last to first give the answer.

Binary, Octal and Hexadecimal Shortcuts

Since 8 is 2 to the power 3 and 16 is 2 to the power 4, each octal digit corresponds to three bits and each hexadecimal digit to four bits. To convert binary to octal, group the bits in threes from the right. To convert to hexadecimal, group them in fours.

Example 1.7: Converting between systems

Problem: Convert decimal 156 to binary, octal and hexadecimal.

Solution:

  1. Repeated division by 2 gives the remainders 0, 0, 1, 1, 1, 0, 0, 1. Read from last to first, the binary number is 10011100.
  2. Group the bits in threes from the right: 010 011 100, which is octal 234.
  3. Group the bits in fours: 1001 1100, which is hexadecimal 9C.

Result: 156 = 10011100 in binary = 234 in octal = 9C in hexadecimal.

n = 156
print(bin(n)[2:], oct(n)[2:], hex(n)[2:].upper())
print(int("10011100", 2), int("234", 8), int("9C", 16))

Output

10011100 234 9C
156 156 156

Common mistakes

  • Reading the remainders from first to last instead of last to first.
  • Grouping bits from the left instead of the right when converting to octal or hexadecimal.
  • Writing hexadecimal digits above 9 as two-digit numbers instead of the letters A to F.

Key takeaways

  • The base is the number of digits in the system.
  • Divide by the base and read remainders in reverse to convert from decimal.
  • Three bits make one octal digit; four bits make one hexadecimal digit.
  • Hexadecimal digits A to F stand for 10 to 15.

Practice questions

  1. Convert binary 1010 to decimal.
  2. Convert hexadecimal 1F to decimal.
  3. How many bits correspond to one hexadecimal digit?

Answers to the practice questions

  1. 1 x 16 + 15 = 31.
  2. Four.

Sources

  • Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit I