https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213
Bug ID: 95213 Summary: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: in-gcc at baka dot org Target Milestone: --- When trying to assign to a bitfield with -Werror=conversion, having a constant mix with a variable causes a "conversion from 'unsigned int' to 'unsigned int:24' may change value", where as alternate formulations with two constants or two variables work fine. This error seems to be present on every version of gcc ever (the ones I tried since 4.9.1, at least one in each major version, all did the same thing). Watch it fail on godbolt: https://gcc.godbolt.org/z/vavr-e struct foo { unsigned int a:8; unsigned int b:24; }; void bar(struct foo num, unsigned int x) { num.b = (5U << 1) | (1 & 1); unsigned int z = 5U; num.b = ((z << 1) | (x & 1)) & 0xffffffU; num.b = ((5U << 1) | (x & 1)) & 0xffffffU; num.a = (unsigned char)x; } In the above example, the last assignment of num.b fails to compile where the other two succeed. Note in the last example, casting the bit (x&1) to _Bool will fix the problem, but is obviously not portable to larger expressions (e.g. x&7).