When this test is compiled and run using -O2 with gcc 4.2 and with mainline, it fails (tested on i686-pc-linux-gnu). It succeeds with 4.1.
class s { public: s(long long aa) : a(aa), i1(0) { } long long id() const { return (this->a << 16) >> 16; } bool operator< (s sv) { return this->a < sv.id(); } private: long long a : 48; int i1 : 16; }; s g(1); int main(int, char**) { if (g < (1LL << 38) - 1) return 0; else return 1; } I haven't pinned down the exact problem. However, it appears to be due to the fact that C++ creates the bitfield as a 48-bit signed integer type. The left shift is done in that 48-bit type, not in the long long type which should be used. The left shift and right shift in the 48 bit yield -1, which is incorrect; it should yield ((1LL << 38) - 1). -- Summary: Miscompiled shift of C++ bitfield Product: gcc Version: 4.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: ian at airs dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33819