Possible Duplicate:
Is there a printf converter to print in binary format?
Here is my program
#include<stdio.h>
int main ()
{
int i,a=2;
i=~a;
printf("a=%d\ni=%d\n",a,i);
return 0;
}
The output is
a=2
i=-3
I want this to print in binary. There are...
%x
(hexadecimal)%o
(octal)%d
(decimal)...but what is for printing binary in printf?
printf() doesn't directly support that. Instead you have to make your own function.
Something like:
while (n) {
if (n & 1)
printf("1");
else
printf("0");
n >>= 1;
}
printf("\n");