AD1

Monday, 6 March 2017

From decimal to binary

Write a C program to convert an integer from decimal to binary , using logical bitwise operators

solution
#include <stdio.h>
int main()
{
unsigned int x;
printf("enter any positive integer\n");
fflush(stdout);
scanf("%d",&x);
//sizeof(int)=4 byte on my machine
for(int i=0;i<=31;i++)
{
if(x&(0x8000>>i)){printf("%d",1);}
else {printf("%d",0);}
}
return 0;
}
view raw dtob.c hosted with ❤ by GitHub



No comments:

Post a Comment