Number system & conversion in Computer Science
There are several methods to define the number system. The method of writing, representing numbers, or representing the same number in different ways is called the number system
There are two types of number systems.
Non-Positional Number System
Positional Number System
Non-Positional Number System:
A number system in which the value of a number does not depend on the position of the digits used in the number is called a non-positional number system. In ancient times, people used to count by drawing on the cave for counting. In this type of counting the value of the number does not depend on its position. So this type of number system is called Non-Positional Number System.
For example-
Positional Number System:
A number system in which the value of a number does depend on the position of the digits used in the number is called a non-positional number system. There are four types of positional numbers.
Binary Number
The base of the binary number system is 2. Because in this system there are two numbers, 0 and 1. The two numbers 0 and 1 are called binary digits or bits. 8 bits together are called a byte and 4 bits together are called a Nibble. In the binary number system, an integer is expressed as a series of 0 and 1. For example — 1010, 1001, 0000, 0001, etc.
Decimal Number
The base of the decimal number system is 10. This method has a total of 10 numbers from 0 to 9. Decimal notation is the word used to describe the method of representing decimal numbers in base 10. This number system is widely used in computer applications.
In the decimal system, each digit has a position and it is 10 times more than the previous one. If the number 25 is a decimal, then 2 is 10 times more than 5. Some examples of decimal numbers are- 63, 45, etc.
Octal Number
The base of the octal number system is 8. This method has a total of 8 numbers from 0 to 7.
Octal is used to shorten binary integers. Sort all binary digits into sets of three, beginning on the right. Add a zero if the last group on the left lacks three digits. Each three-digit binary group corresponds to an octal number with one digit.
Let’s begin with a binary number:
01011101
Make threes out of the binary number. If required, add a zero to the left:
001–011–101
Each three-digit group should be changed into an octal number.
1–3–5
Add the numbers together to get the octal number:
135
Hexadecimal Number
The base of the decimal number system is 16. This method has a total of 16 numbers and symbols. Hexadecimal numbers- 0,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F.
A single hexadecimal digit represents four binary bits called a nibble, which is half of an octet (8 bits). The counting is quite similar to that of decimals, even though the hexadecimal number progression starts with 0 and finishes with F. As a result, when the last system of numbers is reached, the place value is increased to the left, and the current value is set to 0.
Examples of hexadecimal numbers — 12C, 2EC5, 559, etc.
Now a question may arise in your mind that the examples of the 4 number systems look similar and some numbers are precisely the same. For example, 101, 10, and 110 are in the four-number system. Now how can I understand which is an example of which number system?
In fact, for this, the Example is written with the base of the number. For example - (101)₂ , (1001)₁₀ , (56)₁₀ , (567)₈ , (5966)₁₀ , (5966)₁₆
Number system Conversion methods
Number system conversions deal with the operations to change the base of the numbers. A number can be converted from one number system to another number system using number system formulas.
Decimal to other bases
For converting decimals to other bases, Simply divide the decimal number by the new bases’ converted value.
Decimal to Binary
To convert from Decimal to Binary, first divide the Decimal Number by 2 to find the quotient and quotient. And by using this same method every time divide the quotient by 2 to get the quotient and reverse all those quotients together, it will become a Binary Number.
Decimal to Octal
To convert from Decimal to Octal, first divide the Decimal Number by 8 to find the quotient and quotient. And by using this same method every time divide the quotient by 8 to get the quotient and write all those quotients together in reverse and it will become an Octal Number.
Decimal to Hexadecimal
To convert from Decimal to Hexadecimal, first divide the Decimal Number by 16 to find the quotient and the quotient. And by using this same method every time divide the quotient by 16 to get the quotient and reverse all those quotients together, it will become a Hexadecimal Number.
Binary to other bases
Binary to Decimal
To convert from Binary to Decimal, first count the Digits of the Binary Number (Count must count from 0 and count from left to right.) Each Digit must be multiplied by 2ⁿ where n = Base(Count of the digit) of that Digit. Then add all the numbers.
Binary to Octal
There are two ways we can convert binary to octal.
We can convert binary numbers to decimal numbers and then decimals to octal.
Also, it is possible to directly translate from octal to binary and from binary to octal by grouping the digits.
Here are the steps for the grouping system -
Step 1: Identify the binary number, the digits should be either 0 or 1 with base 2.
Step 2: Group all the 0 to 1 in a set of three starting from the right side.
Step 3: Add 0’s to the left if it does not form a group of three. Each group must have three digits.
Step 4: Look at the binary to octal conversion table to get the accurate numbers.
Step 5: Once obtained, that number is the octal number
Binary to Hexadecimal
There are two ways we can convert binary to hexadecimal.
We can convert binary numbers to decimal numbers and then decimals to hexadecimal.
Also, it is possible to directly translate from octal to hexadecimal and from binary to hexadecimal by grouping the digits.
Here are the steps for the grouping system -
Step-1: In the case of integers, the number should be grouped by 4-bits from right to left and in the case of fractions, it should be grouped by 4-bits from left to right.
[For integers less than 4-bits, fill 4-bits by placing the required number of zeros on the left side and fill 4-bits by placing the required number of zeros on the right side in case of fractions]
[The reason for grouping on the left in the case of integers is that putting an extra zero on the left does not change the value.
Step 2: Then write the hexadecimal value of each 4-bit group separately.
[ Add the local values of the number of 1’s in each binary group to get the equivalent hexadecimal value of that binary group]
Step 3: Finally, by arranging the hexadecimal values side by side, the hexadecimal number equivalent to the binary number will be obtained.
Octal to other bases
Octal to Decimal
To convert from Octal to Decimal, first count the digits of the Octal Number (Count must be from 0 and count from left to right.) Each Digit must be multiplied by 8ⁿ where n = Base of that Digit.
Octal to Binary
Steps to convert octal numbers into binary -
Step 1: Write the three-bit binary value of each digit of the octal number.
[If the binary value of each digit is less than 3 bits, 3 bits must be filled by placing the required number of zeros on the left side. Because each digit is written with three bits, each digit of an octal number can be represented by a maximum of three bits.
Step 2: Finally, if you write the obtained binary values side by side, you will get the binary number equivalent to the octal number.
Octal To Hexadecimal
The following two ways to convert octal to hexadecimal number-
First octal to decimal then decimal to hexadecimal
⇒ (1507)₈ // Octal number ⇒ (839)₁₀ // Decimal ⇒ 347₁₆ // Hexadecimal
First octal to binary then binary to hexadecimal
⇒ (1507)₈ // Octal number ⇒ (10111100011)₂ // Decimal ⇒ 347₁₆ // Hexadecimal
Hexadecimal to other bases
Hexadecimal to Decimal
To convert from Hexadecimal to Decimal, first count the Digits of the Hexadecimal Number (Count must count from 0 and count from left to right.) Each Digit must be multiplied by 16ⁿ where n = Base of that Digit.
Hexadecimal to Binary
Steps to convert hexadecimal to binary -
Step 1: Write the four-bit binary value of each digit of the hexadecimal number. [using the formula 8 4 2 1]
[If the binary value of each digit is less than 4 bits then 4 bits should be completed by placing the required number of zeros on the left side. Each digit of a hexadecimal number can be represented by a maximum of four bits because each digit is written in four bits.
Step 2: Finally, if the obtained binary values are written side by side, the binary number equivalent to the hexadecimal number will be obtained.
Hexadecimal to Octal
The following two ways to convert hexadecimal to octal numbers-
First hexadecimal to decimal then decimal to octal
⇒ (347)₁₆ // Octal number ⇒ (839)₁₀ // Decimal ⇒ (1507)₈ // Hexadecimal
First hexadecimal to binary then binary to octal
⇒ 347₁₆ // Octal number ⇒ (10111100011)₂ // Decimal ⇒ (1507)₈ // Hexadecimal
Conclusion
As you probably saw, in many articles the number of conversions was in other orders and you can find that useful. Here I just tried to order it by type.
I hope you liked this quick guide, don’t hesitate to 👏 the article if you found it interesting!
Happy reading and fun coding! 😀