- 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 2^{0} , next bit will have an order of 2^{1} and so on.

^{0}, Bit 1 multiplied with 2

^{1}, Bit 2 multiplied with 2

^{2}and 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 2^{4} with 2^{0} begin the first :

Order / Multiplication Factor(MF) | 2^{4} |
2^{3} |
2^{2} |
2^{1} |
2^{0} |

Bit Value at each Position | 1 | 0 | 0 | 1 | 1 |

Value times the Order/MF | 2^{4} x1 =16 |
2^{3} x0 =0 |
2^{2} x0 =0 |
2^{1} x1 =2 |
2^{0} x1 =1 |

Sum | 16+0+0+2+1 =19 |

Now we simply add the above products to get Decimal of 01011.

*Decimal_of*(01011) = 16+0+0+2+1 = 19**Similar to above is conversion from Binary to Hexadecimal as follows:**

**A group of 4 bits is called a Nibble and that of 8 bits is called a Byte.**So in hexadecimal basically we deal with

**Nibbles**.

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 2^{3} , 2^{2} , 2^{1} and 2^{0} 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 |

*Hexadecimal_of*(1010101001110) = 2A4E**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 |

*Binary_of*(B39F1A) = 101100111001111100011010**Note**that each digit is replaced with exact 4 bits .. i.e 3 is replaced by 0011 and not ’11’. Replacing it with ’11’ will change the value of the number. Also this method is NOT applicable for conversion from Decimal to binary.

Pingback: Tutorial : Embedded programming basics in C – bitwise operations : OCFreaks!()

Pingback: LPC2148 GPIO Programming Tutorial : OCFreaks!()