How it works...

You will be prompted to enter a binary number. The number you enter will be assigned to the bin variable. The binary number is temporarily assigned to a temp variable. A while loop is executed until the binary number in the bin variable becomes 0.

Let's assume that the binary number entered in the bin variable is 1101. Then, we will apply the mod (%) operator to the binary digits in the bin variable in order to isolate its last bit. In fact, the % operator divides by the specified number and returns the remainder. That is, when % 10 is applied to 1 1 0 1, it will return 1which is then assigned to the num variable.

A topower variable is initialized to 0. The purpose of the topower variable is to left-shift the digits, that is, to multiply the binary digits by the power of 2. The binary digit 1 in the num variable is added to another variable called dec. The value of the topower variable is incremented to 1. The binary number 1 1 0 1 in the bin variable is truncated to 1 1 0 by dividing it by 10 and removing the fraction. 

Again, the whole procedure is repeated. The last digit in the bin variable is isolated by the application of the %10 operator; that is, 0 will be isolated from 1 1 0 and assigned to the num variable. The binary digit 0 is left-shifted by 1 bit, making it 0 0. So, a value of 0 is then added to the value in the dec variable; that is, the value in the dec variable remains 1. The value of topower is incremented to 2. The last digit of the binary digit 1 1 0 in the bin variable is removed by dividing it by 10; therefore, the binary digit in the bin variable will become 1 1.

Once again, apply %10 to 1 1; the remainder will be 1which will be assigned to the num variable. The binary digit 1 is left-shifted by 2 bits, making it 1 0 0. The binary value 1 0 0 represents 4which is then added to the value in the dec variable. The value in the dec variable was 1, and after adding 4 to it, the total in the dec variable will become 5. Again, the value in the topower variable will be incremented, making its value 3. The last digit of the binary digits (1 1) in the bin variable will be truncated by dividing it by 10. Hence, the digit in the bin variable will become 1.

Again, %10 is applied to binary digit 1 in the bin variable. As a result of this, 1 will be assigned to the num variable. The binary digit 1 in the num variable is left-shifted by 3 bits, making it 1 0 0 0. The binary value 1 0 0 0 represents 8which is then added to the value in the dec variable. The current value in the dec variable is 5. On adding 8 to it, the value in the dec variable will become 13. The value of the topower variable is incremented to 4. The binary value 1 in the bin variable is divided by 10making it 0. The while loop will terminate and the decimal value 13 in the dec variable is displayed on the screen.

The whole procedure can be illustrated as follows:

Figure 16.1

Let's use GCC to compile the binintodec.c program as follows:

D:\CBook>gcc binintodec.c -o binintodec

If you get no errors or warnings, this means the binintodec.c program has compiled into an executable file, binintodec.exe. Let's run this executable file as follows:

D:\CBook>binintodec
Enter the binary number: 1101
The decimal of 1101 is 13

Voilà! We've successfully converted a binary number to a decimal using a bitwise operator. Now, let's move on to the next recipe!