In the following testcase ------------------- #include <stdio.h> #include <stdint.h> int main() { uint32_t b = 32; printf ("%X\n", ~(-(UINT32_C(1) << b))); return 0; } -------------------
when compiled with -O0 results in 0, while the following testcase ------------------- #include <stdio.h> #include <stdint.h> int main() { printf ("%X\n", ~(-(UINT32_C(1) << 32))); return 0; } ------------------- when compiled with -O0 results (correctly, I might add) in FFFFFFFF as expected. Also the first case when compiled with at least -O1 results in correct result (because it (correctly) optimizes into the second case (a constant)). Problem is that the shifting wraps around while it shouldn't. You can test it by changing the value of 'b'. This happens with _all_ GCC versions I have currently at my disposal, i.e., 4.1.0 20050817 (experimental), 4.0.2 20050804 (prerelease), and 3.4.3 (Mandrakelinux 10.2 3.4.3-7mdk), on both x86 and x86_64 architectures. -- Summary: Shifting wraps around => miscompilation Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: critical Priority: P1 Component: translation AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: drab at kepler dot fjfi dot cvut dot cz CC: gcc-bugs at gcc dot gnu dot org GCC target triplet: i?86-*-*, x86_64-*-* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23540