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 . It uses digits from to . 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 digits: . And we constrain any number in such a way: if we reach at the end of the number while counting, then we increase by the second digit from the end and replace with .
The octal number system works exactly the same, except now we only have digits . Let's count a bit in the octal system:
Why? Because in the octal number system is the last digit – exactly like in the decimal one. We've reached the last digit at the end of the number. And so, we need to increase by the second digit from the end (it was , now it turns into ) and replace with .
A subscript under a number usually represent the base of a number system. In our example, means that the number is written in the octal, or base-, number system. Notice that, for example, !
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 . It consists of times , times , and times :
i.e. each digit of the decimal number is multiplied by (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 ).
Likewise in the octal system, the number is interpreted as:
If we perform the necessary computations, we will find, how this number is written in the decimal number system.
Decimal to octal
So, we have understood that each digit of the octal number gives us information about how many '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 's of each power are contained in a decimal number. To do that we divide our decimal number by until the quotient of the division is . Let's look again at the decimal number .
We take as input and carry out a sequence of divisions with remainder:
- divided by gives with a remainder of ,
- divided by gives with a remainder of ,
- divided by gives with a remainder of ,
- divided by gives with a remainder of .
We then read the sequence of remainders (which are all between and ) backwards, which gives us the number . This is the octal representation of .
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:
It'll take some time to count how many digits are there. And modern computers operate with -digit numbers! So, people decide to split binary numbers into groups of bits. The smallest -bit group is , the largest group is . But wait! It means that each -bit group in a binary number is essentially the same as a single digit in the corresponding number, since !
Below the relationship between binary and octal numbers is clearly visualized:
In the example above, you can see that the leftmost group consists of just one bit: . Fortunately, that is not a problem because we can always transform that one bit to bits by adding two insignificant s to the left of it.
But if we grouped bits from left to right, we cannot just simply add s 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.
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 But, just in case you don't remember, here is a table for your convenience:
| Octal | Binary |
|---|---|
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:
Conclusion
To sum up:
- The octal system is a positional number system with the base that uses digits
- To convert an octal number to the decimal number system, sum up all its digits multiplied by the raised to the power of the digit's position in the number (from right to left, starting with ).
- To convert a decimal number to the octal number system, divide it by until the quotient of the division is . 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 -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.