- Binary and Hexadecimal Number Systems.
- Interconversion of Binary and Hexadecimal.
- Bit Level Operations.
- Pointers , etc..
This tutorial mainly deals with Hexadecimal & Binary Numbering Systems and how to inter-convert them. This lays the foundation for bit level operations. A complete tutorial for bit level(or bitwise) operations is located @ www.ocfreaks.com/tutorial-embedded-programming-basics-in-c-bitwise-operations/.
Numbering Systems & Interconversion
Hexadecimal System / Base-16
The Hexadecimal number system is famous in computing world specially in digital electronics. Its base-16 because it uses 16 symbols to represent any number and each digit has an associated multiplication factor which is a power of 16. Its basically a ‘compact’ numbering system in which few digits are required to represent a sufficiently big number as compared to Decimal system. A Hexadecimal number can readily converted into a binary number and vice-verse. Digits in Hexadecimal systems are 0 to 9 and A,B,C,D,E,F. Each digit in a hexadecimal system can be represented using 4 binary digits. The order of the digits is in increasing power of 2. We will see Hexadecimal in detail but first lets go through Decimal and Binary numbering system & its conversion from binary to decimal.
Decimal System / Base-10
Decimal Numbering system or Base-10(technically) system : This the numbering system we use in our daily lives. Each digit in a decimal number has an associated ‘weight’ which is nothing but the base raised to the location number of the digit. Its Base-10 because it uses a combination of 10 ‘symbols’ or say digits(0,1,2,3,4,5,6,7,8,9) to represent any number.
Consider a decimal number 2734 : By default the digit on extreme RIGHT is called LSD or Least significant digit. In out case its ‘4’. While the digit on extreme LEFT is called MSD or Most Significant Digit i.e ‘2’ – It most significant because the change in digit at that location gives big changes in value of the number. Same argument is applicable for LSD. As you know that Units place has a multiplication factor of 1 , Tens has that of 10 , Hundreds has that of 100 and soo on.. similar to this we have multiplication factor for each digit in binary number system as well.
Binary System / Base-2
Binary Numbering system or Base-2 system: It used only 2 symbols(called bits) ‘0’ & ‘1’ to represent any number. On this Numbering or Counting System is computer understandable and hence most important for embedded programming though its very simple to understand. Binary system can be best understood by using an example to convert Binary to Decimal.
Lets take an example and try to understand binary to decimal conversion :
Consider a binary number say : 10011 which needs to be considered into decimal. Each bit in the binary number carries a specific multiplication factor or say ‘weight’ or say ‘order’. By default the bit on extreme right is starting or the 0th bit and that on the extreme left is last bit. Hence the 1st bit (technically – 0th since in computing counting starts from 0 and not 1 hence as called ‘0 indexed’ system) from the right will have an order of 20 , next bit will have an order of 21 and so on.
After this we simply add all products to get the converted number which is in Decimal.
In our case we have 5 digits hence our last power of 2 will be 24 with 20 begin the first :
Order / Multiplication Factor(MF) | 24 | 23 | 22 | 21 | 20 |
Bit Value at each Position | 1 | 0 | 0 | 1 | 1 |
Value times the Order/MF | 24 x1 =16 | 23 x0 =0 | 22 x0 =0 | 21 x1 =2 | 20 x1 =1 |
Sum | 16+0+0+2+1 =19 |
Now we simply add the above products to get Decimal of 01011.
Similar to above is conversion from Binary to Hexadecimal as follows:
First , Lets have a look at the conversion chart below for Decimal, Hexadecimal and Binary:
Decimal | Hexadecimal | Binary |
0 | 0 | 0000 |
1 | 1 | 0001 |
2 | 2 | 0010 |
3 | 3 | 0011 |
4 | 4 | 0100 |
5 | 5 | 0101 |
6 | 6 | 0110 |
7 | 7 | 0111 |
8 | 8 | 1000 |
9 | 9 | 1001 |
10 | A | 1010 |
11 | B | 1011 |
12 | C | 1100 |
13 | D | 1101 |
14 | E | 1110 |
15 | F | 1111 |
Also the binary representation of any hexadecimal ‘symbol’ or ‘digit’ has only 4 ‘orders’ or ‘weights’ or ‘multiplication factor’ associated. These are 23 , 22 , 21 and 20 or simply 8-4-2-1.
Consider a binary number : 1010101001110 , we can group it as 10-1010-0100-1110. Note that the grouping into 4 starts from right side. Now the group on the extreme left has only 2 bits so we can append zeros to keep things straight forward.
Hence we get : 0010-1010-0100-1110 , Now we replace each group by its Hex representation.
Binary => | 0010 | 1010 | 0100 | 1110 |
Hexadecimal => | 2 | A | 4 | E |
Converting Hexadecimal to Binary: This is as simple as counting 1,2,3 :P. Simply replace each Hexadecimal digit with its binary equivalent.. Thats it!
For eg. consider a Hex number say B39F1A
Replacing Each Hex digit with its binary equivalent we get :
Hexadecimal Digit | B | 3 | 9 | F | 1 | A |
Binary equi. of each Hex Digit | 1011 | 0011 | 1001 | 1111 | 0001 | 1010 |
Discover more from OCFreaks!
Subscribe to get the latest posts sent to your email.