MathComputational mathNumeral systems

Octal numbers

7 minutes read

As you already know, a bit is the unit of measurement of information in computer science. However, it is not very convenient to work with such a binary system directly. Therefore, people decided to group the bits. The most famous grouping method is collecting bits into groups of three or four. Hence, respectively, two coding systems were created – the octal system and the hexadecimal system. Today we'll focus on the first one.

Octal number system

The octal number system is a positional number system with a base of 88. It uses 88 digits from 00 to 77. If it is a bit confusing now, don't be scared. Let's take a look at how it works.

In the decimal system, we use 1010 digits: 0,1,290,1,2\dots9. And we constrain any number in such a way: if we reach 99 at the end of the number while counting, then we increase by 11 the second digit from the end and replace 99 with 00.

The octal number system works exactly the same, except now we only have digits 0,1,270,1,2\dots7. Let's count a bit in the octal system:

0,1,2,3,4,5,6,77+1=1080,1,2,3,4,5,6,7\dots 7 + 1 = 10_8Why? Because 77 in the octal number system is the last digit – exactly like 99 in the decimal one. We've reached the last digit at the end of the number. And so, we need to increase by 11 the second digit from the end (it was 00, now it turns into 11) and replace 77 with 00.

A subscript under a number usually represent the base of a number system. In our example, 10810_8 means that the number 1010 is written in the octal, or base-88, number system. Notice that, for example, 577857710577_8 \ne 577_{10}!

Octal to decimal

Now let's learn how to convert numbers from the octal number system to the decimal.

Let's look at the decimal number 571571. It consists of 55 times 100100, 77 times 1010, and 11 times 11:

571=5102+7101+1100571 = 5 \cdot 10^2 + 7\cdot10^1 + 1\cdot 10^0i.e. each digit of the decimal number is multiplied by 1010 (which is, remember, the base of the decimal system) raised to the power of its position in the number (from right to left, starting with 00).

Likewise in the octal system, the number 5718571_8 is interpreted as:

5718=582+781+180571_8 = 5\cdot 8^2 + 7\cdot 8^1 + 1\cdot 8^0If we perform the necessary computations, we will find, how this number is written in the decimal number system.

5718=582+781+180=564+78+1=37710571_8 = 5\cdot 8^2 + 7\cdot 8^1 + 1\cdot 8^0 = 5\cdot 64+7\cdot8+1 = 377_{10}

Decimal to octal

So, we have understood that each digit of the octal number gives us information about how many 88's raised to the power of the digit's position are in this number. But that means, now we know how to convert numbers from the decimal to the octal system! We just need to count how many 88's of each power are contained in a decimal number. To do that we divide our decimal number by 88 until the quotient of the division is <8<8. Let's look again at the decimal number 571571.

Decimal to octal

We take 571571 as input and carry out a sequence of divisions with remainder:

  • 571571 divided by 88 gives 7171 with a remainder of 33,
  • 7171 divided by 88 gives 88 with a remainder of 77,
  • 88 divided by 88 gives 11 with a remainder of 00,
  • 11 divided by 88 gives 00 with a remainder of 11.

We then read the sequence of remainders (which are all between 00 and 77) backwards, which gives us the number 10738=183+781+3801073_8=1\cdot 8^3 + 7\cdot8^1 + 3\cdot8^0. This is the octal representation of 57110571_{10}.

From binary to octal (and back again)

As we have said at the beginning of this topic, the octal system was invented because of the inconvenience of the binary representation for humans. Look at this example:

100111001011000121001110010110001_2It'll take some time to count how many digits are there. And modern computers operate with 6464-digit numbers! So, people decide to split binary numbers into groups of 33 bits. The smallest 33-bit group is 0002=010000_2=0_{10}, the largest group is 1112=710111_2=7_{10}. But wait! It means that each 33-bit group in a binary number is essentially the same as a single digit in the corresponding number, since 8=238=2^3!

Below the relationship between binary and octal numbers is clearly visualized:
1 001 110 010 110 001 21162618\underbrace{1}~\underbrace{001}~\underbrace{110}~\underbrace{010}~\underbrace{110} ~\underbrace{001} ~_2\\1\quad1\quad6\quad2\quad6\quad1_8

Grouping starts from right to left! Let's see why grouping direction is extremely important:

In the example above, you can see that the leftmost group consists of just one bit: 11. Fortunately, that is not a problem because we can always transform that one bit to 33 bits by adding two insignificant 00s to the left of it.

But if we grouped bits from left to right, we cannot just simply add 00s to the right of our number – the number will change! Look at the example below. In the first formula, grouping starts from right to left, which is correct. In the second formula, it starts from left to right, and adding two zeros to the rightmost group (whether on the left or on the right) would change the initial number.

10112=1 011 2=001 011 210112=101 1 2101 001 21011_2 =\underbrace{1} ~ \underbrace{011}~_2 = \underbrace{001} ~ \underbrace{011}~_2 \\ 1011_2 =\underbrace{101} ~ \underbrace{1}~_2 \ne \underbrace{101} ~ \underbrace{001}~_2 \\

And now we just replace each binary triplet with its octal value. By the way, here octal value is the same as decimal value, since the maximal value of a binary triplet is 1112=78=710111_2=7_8=7_{10} But, just in case you don't remember, here is a table for your convenience:

Octal Binary
080_8 0002000_2
181_8 0012001_2
282_8 0102010_2
383_8 0112011_2
484_8 1002100_2
585_8 1012101_2
686_8 1102110_2
787_8 1112111_2

To, the other way around, convert an octal number to the binary number system, just replace each octal digit with the corresponding binary triplet from the table and remove leading zeros, if necessary. For example: 25418=[28584818]=[0102101210020012]=1010110000122541_8=[2_8|5_8|4_8|1_8]=[010_2|101_2|100_2|001_2]=10101100001_2

Conclusion

To sum up:

  • The octal system is a positional number system with the base 88 that uses digits 0,1,270,1,2\dots7
  • To convert an octal number to the decimal number system, sum up all its digits multiplied by the 88 raised to the power of the digit's position in the number (from right to left, starting with 00).
  • To convert a decimal number to the octal number system, divide it by 88 until the quotient of the division is <8<8. Then write down the final quotient and after it all remainders in reverse order.
  • To convert a binary number to the octal number system, split it into 33-digits groups (starting from the right) and replace each triplet with the corresponding octal number.
  • To convert an octal number to the binary number system, replace each octal digit with the corresponding binary triplet.
232 learners liked this piece of theory. 14 didn't like it. What about you?
Report a typo