//declaring function bit(int)
void bit(int);
void main()
{
int val;
printf("Enter the value of integer to print its bit pattern:\n");
scanf("%d",&val);
printf("The bit pattern for integer %d is\n",val);
//printing binary pattern requires the testing of each bit
//so we will test each bit starting from the 15th bit till we reach to left
//this will be achieved by bit(val) function
bit(val);
}
void bit(int n)
{
int i,m;
for(i=15;i<=0;i--)
{
//testing ith bit
//testing ith bit requires masking
//the masking is done through 1<
m=1<
if((n&m)==0)
{
printf("0");
}
else
{
printf("1");
}
}
}
0 comments: