***This tutorial covers basic conversions for number systems. Floating point numbers, sign-extended numbers, fixed point numbers and any of the "compliment" methods will not be mentioned. This section is just a basic understanding of conversions and representations.***
There are many number systems in the world of computer science and mathematics. Some systems include the decimal system (base ten), octal system (base eight), binary system (base two) and hexadecimal system (base sixteen).
The below chart can certainly come in handy as these are most commonly seen when converting numbers. The table is representing the first 11 powers of 2 which includes 2 to the 0th power.
Representations
Number systems each have different representations. For example, each number we see written out is implied to be in decimal unless otherwise stated. For example, we could write the following numbers in decimal this way:
Each number system has what is called a base. As mentioned earlier, decimal is a system of base 10; octal is a system of base 8; binary is a system of base 2 while hexadecimal is a system of base 16. The above numbers in the parenthesis show the base representation for decimal numbers. Either way you write them is acceptable but it is much more conventional to write them without the parenthesis and the base.
The base is also the number of digits or characters used to represent numbers in the system. Below is a chart of the digits or characters each number system uses:
So in turn, the range of digits or characters for each system is: 0 ≤ d < base
Here is a chart showing some decimal numbers and their equivalent representations in each of the mentioned number systems.
Again, each can be written in the base representation. One example is:
Conversions
What happens when someone only knows how to read numbers in a certain way. Most everyone knows how to read decimal numbers. So say we are given a binary number or a hexadecimal number that a person cannot read and understand. We can convert that given number into the appropriate system using a set of rules.
The first type of conversion that is easiest to learn is a binary number to a decimal number.
Binary to Decimal
To take a small example, the binary representation of the decimal number 9 is: (1001)2
The algorithm to go about the conversion is:
So using the above methods, here is the resulting conversion to decimal above:
A shortcut is to avoid counting the 0 bits in the sum. It saves space and time when converting but also remember that you need to increase the counter for the converting otherwise the number will not be correct.
Let's look at another example converting the below binary number: (101101001)2
Using the conversion steps above and skipping the 0 bits, here is the conversion:
Decimal to Binary
A more annoying and longer way of converting numbers is the decimal system to binary system. The best way of doing this is to follow these steps:
The example here with the decimal number 8 is a good way to start:
To check the result, use the previous method of converting from binary to decimal:
Let’s see another example using (125)10
To check the result:
Binary to Hexadecimal
Another easy conversion to do is binary to hexadecimal. The hexadecimal number system uses the digits 0 to 9 and A,B,C,D,E,F. And since the hexadecimal system is a power of 2 (24), we can take a binary number in groups of 4 and use the appropriate hexadecimal digit in it’s place.
The steps to doing so are simple. Begin at the rightmost 4 bits. If there are not 4 bits, pad 0s to the left until you hit 4. Repeat the steps until all groups have been converted.
An example is below:
Take the binary number (1000101). Using the above steps, here is the conversion:
Take the binary number (11101011). Using the same steps, here is the conversion:
Hexadecimal to Binary
This conversion is also simplistic. Given a hexadecimal number, simply convert each digit to it’s binary equivalent. Then, combine each 4 bit binary number and that is the resulting answer.
Take the hexadecimal number (A2F)16 and convert it to it’s binary form:
Take the hexadecimal number (55)16 and convert it to it’s binary form:
Binary to Octal
Since the octal system is again a power of two (23), we can take group the bits into groups of 3 and represent each group as an octal digit. The steps are the same for the binary to hexadecimal conversions except we are dealing with the octal base now.
Take the binary number (10011)2 and convert it to octal:
Take the binary number (10010101011)2 and convert it to octal:
Octal to Binary
To go from octal to binary, simply reverse the above algorithm and represent each octal digit in it’s three bit binary form.
Take the octal number (742)8 and convert it to binary:
Take the octal number (1234567)8 and convert it to binary:
Decimal to Octal
Similar to the decimal to binary method, you must divide each number by 8 and record the remainder. This continues until the number cannot be divided into anymore.
Take the decimal number (18)10 and convert it to octal:
Take the decimal number (46)10 and convert it to octal:
Octal to Decimal
Similar to the binary to decimal method, simply take each digit in the octal base and multiply by the power of 8.
Take the octal number (764)8 and convert it to decimal:
Take the octal number (5771)8 and convert it to decimal: