You will be prompted to enter a decimal value. The decimal value you entered will be assigned to the num variable. Let's assume that you have entered a value of 13. This value will be internally stored in the form of binary digits in the num variable as follows:
We will set a totbits variable to 32 bits because an int data type in C consists of 32 bits, and we have to mask each bit of the number in the num variable to display its binary version. We will define a mask variable and assign a value of 1 to it. To make the value 1 in the mask variable appear as 10000...00, that is, 1 followed by 31 zeros, we will left-shift value 1 by 31 bits as follows:
Now, we will execute a for loop 32 times to mask or isolate each bit in the num variable and display it. Within the for loop, we will apply an AND operation on the num and mask variables. Consequently, each of the binary digits of the two variables will be ANDed. We know that, in the AND operation, the output is 1 only when both of the bits are 1. If either of the bits is 0, the AND operation will return 0.
Figure 16.9 shows the application of the AND operation on the num and mask variables:
Hence, the value 0 is returned. Thereafter, we will right-shift the binary digits in the mask variable by 1 bit, making it 0 1 followed by 30 zeros. Again, when the AND operation is applied between the num and mask variables, the result will be 0 (refer to the following figure), which is then displayed on the screen. So, up until now, we have 0 0 displayed on the screen:
Again, we will right-shift the binary digits in the mask variable by 1 bit, making 0 0 1 followed by 29 zeros. Again, when the AND operation is applied between the num and mask variables, the result will be 0, which is then displayed on the screen.
The procedure and output will be the same for the next 25 bits; that is, we will get 28 zeros on the screen. After that, when we apply another right-shift operation on the binary digits of the mask variable, it will become 1 0 0 0. On the application of the AND operation on the num and mask variables, we will get an output of 1, which is then displayed on the screen. So, for now, we have 28 zeros followed by 1 bit on the screen:
As we keep repeating the procedure, we will get the outputs shown in Figure 16.12. We will have 28 zeros followed by 1 1 bits on the screen (Figure 16.12 (a)). After another repetition, we will have 28 zeros followed by 1 1 0 bits on the screen (Figure 16.12 (b)). In the final execution of the for loop, the final binary version of the number assigned to the num variable will be 28 zeros followed by 1 1 0 1 (Figure 16.12 (c)):
Let's use GCC to compile the decintobin.c program as follows:
D:\CBook>gcc decintobin.c -o decintobin
If you get no errors or warnings, that means the decintobin.c program has compiled into an executable file, decintobin.exe. Let's run this executable file as follows:
D:\CBook>decintobin
Enter decimal value: 13
00000000000000000000000000001101
Voilà! We've successfully converted a decimal number into binary using bit masking.