#include <stdio.h> struct S { unsigned int ui17 : 17; } s; int main() { s.ui17 = 0x1ffff; printf("%x\n", (unsigned int)s.ui17); if (s.ui17 >= 0xfffffffeu) puts("FAIL"); return 0; }
Maybe I don't understand GCC's rules for the promotion of bitfields, but it seems to me that s.ui17 should evaluate to ((unsigned int)0x1ffff), which is less than 0xfffffffeu, so "FAIL" should not be printed. But "FAIL" is printed. I can't explain why; it's almost as if GCC is truncating the rhs to 0x1fffe before doing the comparison! Changing s.ui17 from 17 bits to 15 bits makes the test pass (i.e., not print "FAIL"). I believe in that case GCC promotes s.ui17 to "unsigned short"... but that should undergo usual arithmetic conversion to "unsigned int" at which point we ought to see the same FAIL behavior happening. Reproduced with GCC 4.2.4 and 4.3.3. -- Summary: Comparison involving unsigned int:17 bitfield seems wrong Product: gcc Version: 4.3.3 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: foo at mailinator dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40404