I was reviewing bitwise operators and wrote a simple code to print the binary representation of numbers but I am having crazy output, and I have no explanation for it. why is the program not giving me the correct binary numbers ? Here is the sample output :
and my code :
#include <stdio.h>
#include <stdlib.h>
void pBinary(int x);
int main(void)
{
for (int n = 0; n < 20; n++) {
pBinary(n);
}
return 0;
}
void pBinary(int x)
{
int y = 1 << 31;
for (int n = 0; n < 32; n++) {
x & y ? putchar('1') : putchar('0');
y >>= 1;
}
putchar('
');
}
See Question&Answers more detail:os