http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55658
Bug #: 55658 Summary: bitfields and __attribute__((packed)) generate horrible code on x86_64 Classification: Unclassified Product: gcc Version: 4.7.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: tud...@fb.com Created attachment 28932 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28932 Simple source code to exhibit the problem; disassembly (as produced by objdump -d) I have a structure defined as: struct D { uint64_t id_ : 63; bool x_ : 1; } __attribute__((packed)); uint64_t D_id(const struct D* p) { return p->id_; } The generated code for D_id (using gcc 4.7.1 -O2 on Linux x86_64) builds the return value byte by byte and masks out the most significant bit from the last byte. Removing __attribute__((packed)) and/or changing the structure so it is no longer a bitfield generates sane code. This happens in both C and C++ (unsurprisingly).