C printing bits

You are calculating the result correctly, but you are not printing it right. Also you do not need a second loop:

for(;i<size*8;++i){
    // print last bit and shift left.
    printf("%u ",num&maxPow ? 1 : 0);
    num = num<<1;
}

If you’d like to show off, you could replace the conditional with two exclamation points:

printf("%u ", !!(num&maxPow));

Leave a Comment