Number Systems for Computer Programmers.
Number Systems for
Computer Programmers.
As a computer programmer, you might have to work with different types of number systems. Starting from well known Decimal Number Systems to Binary and Hexadecimal are the most usable number systems in programming.
In this tutorial I’m going to explain what these systems are and how they interact with each other.
Contents:
- Basic Rules and Definitions
- The Decimal Number System
- The Binary Number System
- Two’s-Complement Notation
- The Hexadecimal Number System
- Conversion of One Number System to Another
- From Decimal to Binary
- From Decimal to Hexadecimal
- From Binary to Hexadecimal and Vise Versa
- Decimal Numbers and their Binary and Hexadecimal Equivalents
Basic Rules and Definitions
Before go further I want to point out few basic rules in mathematics, which I’m going to use later in my tutorial.
- Any number to the 0th of power equals to 1.
- Any number to the 1st of power equals to the same number (number itself).
- Any number can be represented as powers of the system’s base.
- The system of base n requires n number of digits.
Following definitions are used;
- Digits of a number is count from right to left and the right most digit is the first digit of that number.
- First digit holds 1st place value, second digit holds 10th place value, third digit holds 100th place value, and so on.
- Most Significant Bit (MSB) is the left most bit of a binary value.
- Least Significant Bit (LSB) is the right most bit of a binary value.
The Decimal Number System
The decimal number system is base-10 system and it is the most common number system that you use everyday. It requires 10 different digits, from 0 to 9.
For an example, 154-is expressed as powers of 10. The digit 4 (first digit) gives 10 to the 0 power, digit 5 (second digit) gives 10 to the 1 power, and so on.
Number 154 can be deploying as follows.
Code:
1 1 x 102 = 1 x 100 = 100
5 5 x 101 = 5 x 10 = 50
4 4 x 100 = 4 x 1 = 4
Sum = 154 (Decimal)
Mathematical representation of 154 in base-10 is, 15410. But it can be written as 154 without any uncertainty.
The Hexadecimal Number System
The hexadecimal system (also called as hex system) is base-16, so that it requires 16 different digits. The digits from 0 to 9 are used, along with letters from A to F, which represent the decimal values from 10 to 15 respectively.
1AF is an example for hexadecimal number. Its decimal format;
Code:
1 1 x 162 = 1 x 256 = 256
A A x 161 = 10 x 16 = 160
F F x 160 = 15 x 1 = 15
Sum = 431 (decimal)
Standard representation of the above hexadecimal number is 1AF H; put the letter ‘H’ at the end.
The hexadecimal system is also useful in computer works, because it’s based on power of 2.
Each digit in the hexadecimal system is equivalent to four digits in the binary system, two digits in the hexadecimal system is equivalent to eight digits (1 byte) in the binary system, and so on.
Example:
Binary value 111110102 holds two hexadecimal values. One is by 10102 and the other one by 11112. In hexadecimal they represent A H and F H respectively.
So, 111110102 = FA H
Conversion of One Number System to Another
Now you already know that how to convert binary and hexadecimal values into decimal values. So I don’t want to discuses it again and make my tutorial so bulky and unpleasant to you.
First I’ll explain that how to convert a decimal value into binary and hexadecimal value. And then explain that how to convert a binary value into hexadecimal and vise versa.
From Decimal to Binary
Divide the given decimal number by 2, until can’t do further dividing. And at the same time write-down the remainder values respectively. First remainder value is the least significant bit (LSB) and the last remainder value is the most significant bit (MSB) of the derived binary number, from the given decimal number.
From Decimal to Hexadecimal
Divide the given decimal number by 16, until can’t do further dividing. Then follow the same procedure used to derive the binary number from decimal number.
From Binary to Hexadecimal and Vise Versa
To convert binary number into hexadecimal number, group the binary number into four bits segments. If there is not enough bits to do that it is allow you to add zeros before the most significant bit. Then find the appropriate hexadecimal number through the decimal number of the each four bits segment. It’s not difficult. Just you have to keep in mind 00002 to 11112 binary values, along with respective hexadecimal values from 0 H to F H. Those values are included in table “Decimal Numbers and their Binary and Hexadecimal Equivalents”.
Example:
Code:
111110102 = (1111 1010)2 = FA H
100111012 = (1001 1101)2 = 9D H
1010112 = (0010 1011)2 = 2B H
If you want to convert a hexadecimal number into binary number, just find the relative binary number through decimal numbers for each single digits of hexadecimal number.
Decimal Numbers and their Binary and Hexadecimal Equivalents
The following table state that the decimal numbers with there equivalent binary and hexadecimal values.
Code:
Decimal Number Binary Equivalent Hexadecimal Equivalent
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 00010000 10
17 00010001 11
50 00110010 32
240 11110000 F0
250 11111010 FA
255 11111111 FF
That's all about the number systems. Hope you gain a tiny. If you think that there should any changes, or suggestions please feel free to send it to me.
Thanks,
Best regard,
Eranga.