http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46517
Summary: Wrong result on left shift operator without
optimization
Product: gcc
Version: 4.4.4
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
The next samples compiled without opimisation give not the same result
in both case if it's use has constante the result is good
(the test is on intel platfom)
#> gcc -o shift shift.c
result:
constant val=0
variable val=1
#> gcc -O2 -o shift shift.c
result:
constant val=0
variable val=0
On the first case is not a shift but it's rotate
you can verify by changing the constant:
example SHIFT = 34 the the result is
constant val=0
variable val=4
---->8---->8---->8---->8---->8
#include <stdio.h>
#define SHIFT (4*8)
int main(int argc, char* argv[])
{
int val = SHIFT;
printf("constant val=%x\n", (1<<SHIFT));
printf("variable val=%x\n", (1<<val));
return 0;
}
---->8---->8---->8---->8---->8