Using unsigned
to represent a mask for first len
bits, I wrote a function as below:
void left_shift(int len) {
unsigned x = ~0u << (32 - len);
printf("%08x\n", x);
}
However, when I call left_shift(0)
the console prints ffffffff
, rather than 00000000
that I expected.
So, I made another function to test whether I was misunderstanding the bit-shift operation as below:
void left_shift() {
unsigned x = ~0u << (32 - 0);
printf("%08x\n", x);
}
This time, it prints the expected 00000000
.
To conclude:
int main() {
left_shift(); // prints "00000000"
left_shift(0); // prints "ffffffff"
return 0;
}
I'm really confusing. Could anyone help explain?