A number system is simply a system that we use to represent numbers of a certain type. We use number systems to keep track of ‘things’ in different scenarios. Examples of number systems include the Roman numeral number system [I, II, III, IV, …], the decimal number system[1, 2, 3, 4, 5, …], and the binary system[10, 101, 1101, …] to name a few. The Roman numeral system can be used for counting or keeping track of some task. Similarly, the decimal number system can be used for the same thing as the Roman numeral system and more. The binary can be used to store alphabetic characters on a computer and to store data.
A microprocessor is what executes all instructions from users, these instructions need to be in a language that the computer understands. The binary systems is perfect for the computer as it does not have a large number of symbols for representing data. It has only two, 0 and 1 (called binary digits or bits), the 0 representing an off state and the 1 representing an on state. These bits are then grouped together to represent things that are meaningful to us.
There are many types of number systems. The most common one to us is the decimal number system a.k.a base 10. We, humans, use it, the decimal number system, but computers use in addition to the decimal number system, the binary, a.k.a. base 2, and hexadecimal, a.k.a. base 16 number systems.
Some use cases of the different number systems in computers include writing values and instructions to the CPU which is done by the binary system. The hexadecimals are used for defining memory locations, to define HTML and CSS colors and to represent MAC addresses which are used to uniquely identify your machine.
In this article, we will look at the binary, octal and hexadecimal number systems. We will see how each number system looks like and how we translate the decimal numbers to each number system respectively which is essentially what the computer does to represent data in a way that we can understand.
Decimal
These are the numbers that we see everyday i.e. 1, 2, 3, 4, 5… The decimal number system is also known as base 10, probably because humans have 10 fingers and we use those to count. Base 10 includes the numbers 0 to 9 and that means that any number can be represented using the numbers 0 to 9. All decimal numbers are a combination of the numbers 0 to 9.
If we try represent the number 723 in decimal using the table below, you will see that we need 7 hundreds, 2 tens and 3 ones.
Decimal value | 1000000 | 10000 | 10000 | 1000 | 100 | 10 | 1 |
10^6 | 10^5 | 10^4 | 10^3 | 10^2 | 10^1 | 10^0 | |
723 | 7 | 2 | 3 |
To get back 723 you use the formula (7 * 10^2) + (2*10^1) + (3 * 10^0).
Another thing to note is that this number is written from left to right, which is how we normally write numbers and how all the other number system representations will be written as well. The example above shows the methodology of all conversions. We will see more examples below.
Binary
The binary number system is also known as base 2. It uses 2 digits for representation, 0 and 1.
Let’s represent the numbers 23 and 109 in binary.
- First step is to repeatedly divide each number by 2 and take note of the remainders.
23 | 109 |
23 % 2 = 11 rem 1 | 109 % 2 = 54 rem 1 |
11 % 2 = 5 rem 1 | 54 % 2 = 27 rem 0 |
5 % 2 = 2 rem 1 | 27 % 2 = 13 rem 1 |
2 % 2 = 0 rem 0 | 13 % 2 = 6 rem 1 |
0 % 2 = 0 rem 1 | 6 % 2 = 3 rem 0 |
3 % 2 = 1 rem 1 | |
1 % 2 = 0 rem 1 |
2. Once you have done this, you write the binary as all your remainders from the bottom up.
So, 23 in binary will be 10111 and 109 is 1101101. To confirm this, we can use the method we used above with the decimals.
(1 * 2^{0}) + (1 * 2^{1}) + (1 * 2^{2}) + (0 * 2^{3}) + (1 * 2^{4}) = 23
(1 * 2^{0}) + (0 * 2^{1}) + (1 * 2^{2}) + (1 * 2^{3}) + (0 * 2^{4}) + (1 * 2^{5}) + (1 * 2^{6}) = 109
Decimal value | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
2^{8} | 2^{7} | 2^{6} | 2^{5} | 2^{4} | 2^{3} | 2^{2} | 2^{1} | 2^{0} | |
23 | 1 | 0 | 1 | 1 | 1 | ||||
109 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
Octal
The octal number system is also known as the base 8. It uses numbers 0 to 7 for representation. Let’s try represent 456 in octal. The methodology is exactly the same as the one used in binary except, instead of dividing by 2, you will divide by 8.
456 % 8 = 57 rem 0
57 % 8 = 7 rem 1
7 % 8 = 0 rem 0
0 % 8 = 0
456 in octal is 010.
Decimal value | 32768 | 4098 | 512 | 64 | 8 | 1 |
8^{5} | 8^{4} | 8^{3} | 8^{2} | 8^{1} | 8^{0} | |
456 | 0 | 1 | 0 |
Hexadecimal
Hexadecimal is also known as base 16. This number system has 16 symbols i.e. 0 – 9 and the characters A – F. Hexadecimal has the additional characters because sometimes when you divide the decimal number you are trying to convert to hexadecimal, you get a remainder that is bigger than the numbers between 0 and 9. When this happens, you use the one of the characters to represent the remainder. A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.
Let’s represent then number 277 in hexadecimal. Again, the method applied to binary and octal applies here as well.
277 % 16 = 17 rem 5
17 % 16 = 1 rem 1
1 % 16 = 0 rem 1
277 in hexadecimal is 115. We can confirm this by using our table below.
Let’s do one more example where we have letters in our hex digit: 423.
423 % 16 = 26 rem 7
26 % 16 = 1 rem 10 (10 is equivalent to A)
1 % 16 = 0 rem 1
423 in hexadecimal is 1A7.
Decimal value | 65536 | 4096 | 256 | 16 | 1 |
16^{4} | 16^{3} | 16^{2} | 16^{1} | 16^{0} | |
277 | 1 | 1 | 5 | ||
423 | 1 | A | 7 |
In addition to numbers, computers need to process and understand the alphabet, punctuation marks, operators and etc. Because computers only understand numbers, all of these other characters have an alphanumeric code which is the number equivalent of all the symbols and alphabets. There are different kinds of alphanumeric codes which are used for this kind of translation. The most widely used is ASCII (American Standard Code for Information Interchange). Other kinds of alphanumeric codes include ISCII (Indian Standard Code for Information Interchange) which was created to cater for languages in India. Another is Unicode which was created to cater for any kind of language.