I am trying to write a program in C that prints bits of int. for some reason i get wrong values,
void printBits(unsigned int num){
unsigned int size = sizeof(unsigned int);
unsigned int maxPow = 1<<(size*8-1);
printf("MAX POW : %u\n",maxPow);
int i=0,j;
for(;i<size;++i){
for(j=0;j<8;++j){
// print last bit and shift left.
printf("%u ",num&maxPow);
num = num<<1;
}
}
}
My question, first why am i getting this result (for printBits(3)).
MAX POW : 2147483648 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2147483648 214748364 8
second is there a better way to do this ?