How it works...

You will be prompted to enter a decimal number. The number you enter is assigned to the num variable. The value entered in the num variable is temporarily assigned to another variable, temp. A while loop is set to execute until the value of num becomes 0. Apply the logical AND operation to isolate each binary digit of the number. For example, if the value entered in variable num is 13, then, internally, it will be stored in a binary format as follows:

Figure 16.2

Now, the least significant bit is isolated by applying the AND operation. That is, the binary digits of 13 are ANDed with 1 as follows. The ANDed means the AND operation is applied on binary digits of 13 and 1 :

Figure 16.3

The application of AND on binary digits 1 and 1 results in 1. If either of the binary digits is 0, then the result of AND will be 0. So, the output of num AND 1 will be 1, which will be stored into array p at index location 0 as shown in the following screenshot:

Figure 16.4

After that, right-shift the digits in the num variable by 1 bit. On shifting to the right, the least significant bit, 1, will be dropped off and a 0 will be added to the most significant bit. Again, the new set of binary digits in the num variables is ANDed with 1, that is, the AND operation is applied between the new set of binary digits in num  variable and 1. The output of num AND 1 will be 0, which is then assigned to array p at index location 1; that is, 0 will be assigned to the p[1] location as shown in the figure:

Figure 16.5

Again, right-shift the digits in the num variable by 1 bit. Again, the least significant bit, 0will be dropped off and a 0 will be added to the most significant bit. Once more, the new set of binary digits in the num variables is ANDed with 1as shown in Figure 16.6(a). The output of the num variable AND 1 will be 1which is then assigned to array p at index location 2. Thereafter, right-shift the digits in the num variable again by 1 bit. The most significant bit in the num variable, 1will be dropped off and a 0 bit will be added to the most significant bit location. The binary digits in num are once more ANDed with 1 . ANDed here means, the AND operation is applied between the binary digits in num and 1. The output of the AND operation will be 1which will be assigned to array p at index location p[3] (Figure 16.6(b)):

Figure 16.6 (a) and (b)

Now, the binary digits assigned to array p are the binary conversion of the number assigned to variable num. Simply display the binary digits in array p in reverse order to get the result. Hence, 1 1 0 1 is the binary conversion of 13.

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

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

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

D:\CBook>convertintobin
Enter Decimal Number : 13
Binary of 13 is 1101

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