P
pc
Below is a snippet of code and the result (compiler version also
mentioned). I fail to understand why the left shift operator behaves
as if it were the left rotate operator when the operand is a variable.
$ cat test.c
#include <stdio.h>
int main(void)
{
unsigned int a = 32, b = 33;
printf("with constants 1 << 32 = %u, 1 << 33 = %u\n", 1<<32,
1<<33);
printf("with variables 1 << 32 = %u, 1 << 33 = %u\n", 1<<a, 1<<b);
return 0;
}
$ gcc test.c
test.c: In function 'main':
test.c:6:5: warning: left shift count >= width of type
test.c:6:5: warning: left shift count >= width of type
$ ./a.out
with constants 1 << 32 = 0, 1 << 33 = 0
with variables 1 << 32 = 1, 1 << 33 = 2
$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
mentioned). I fail to understand why the left shift operator behaves
as if it were the left rotate operator when the operand is a variable.
$ cat test.c
#include <stdio.h>
int main(void)
{
unsigned int a = 32, b = 33;
printf("with constants 1 << 32 = %u, 1 << 33 = %u\n", 1<<32,
1<<33);
printf("with variables 1 << 32 = %u, 1 << 33 = %u\n", 1<<a, 1<<b);
return 0;
}
$ gcc test.c
test.c: In function 'main':
test.c:6:5: warning: left shift count >= width of type
test.c:6:5: warning: left shift count >= width of type
$ ./a.out
with constants 1 << 32 = 0, 1 << 33 = 0
with variables 1 << 32 = 1, 1 << 33 = 2
$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2