In this recipe, we will learn to convert a decimal number into a binary number by making use of a bitwise operator. Bitwise operators operate on the binary digits of a number and enable us to do precise manipulation as desired.
Converting a decimal into binary using a bitwise operator
How to do it…
To convert a decimal number into a binary number by making use of a bitwise operator, perform the following steps:
- Enter a decimal number. This number is internally stored in the form of binary digits.
- Isolate the least significant bit of the decimal number by applying a logical AND operation between the entered decimal number and value 1.
- The least significant bit that results from step 2 is stored in an array.
- Right-shift the binary digits of the decimal number by 1 bit. On shifting to the right, the second least significant bit will become the least significant bit.
- Repeat steps 2 to 4 until all the binary digits of the decimal number are placed into the array.
- The binary digits assigned to the array are the binary version of the entered decimal number. Display the binary digits in an array to get the result.
The program for converting a decimal into a binary number using a bitwise operator is as follows:
convertintobin.c
#include <stdio.h>
void main()
{
int num,i,x,temp;
int p[10];
printf("Enter Decimal Number : ");
scanf("%d",&num);
temp=num;
x=0;
while(num > 0)
{
if((num & 1) == 0 )
{
p[x]=0; x++;
}
else
{
p[x]=1;
x++;
}
num = num >> 1;
}
printf("Binary of %d is ",temp);
for(i=x-1;i>=0;i--)printf("%d",p[i]);
}
Now, let's go behind the scenes.
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:
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 :
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:
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:
Again, right-shift the digits in the num variable by 1 bit. Again, the least significant bit, 0, will 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 1, as shown in Figure 16.6(a). The output of the num variable AND 1 will be 1, which 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, 1, will 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 1, which will be assigned to array p at index location p[3] (Figure 16.6(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!