Possible Duplicate:
GCC Overflow Overflow
Consider the following minimum program.
#include <stdint.h>
#include <stdio.h>
int main()
{
uint32_t v = 1024;
v &= (((uint32_t)1 << 32) - 1);
printf("v = %u\n", v);
return 0;
}
This prints 1024, as I expected, a compilation with GCC under MinGW. Since 1 is shifted 32 times to the left, again 0, so 0-1 = -1, which is "1111 .... 1111". This AND value with any value should return the same value again.
However, if I change the program to
#include <stdint.h>
#include <stdio.h>
int main()
{
unsigned int s = 32;
uint32_t v = 1024;
v &= (((uint32_t)1 << s) - 1);
printf("v = %u\n", v);
return 0;
}
Now the printed result 0. Can anyone explain this behavior?
source
share