3 ms·
Bit fields are a specific C language feature, allowing to treat bit slices as a unit whose length doesn’t need to correspond to one of the integer types. See fo
by layer8 5y ago
Bit fields are a specific C language feature, allowing to treat bit slices as a unit whose length doesn’t need to correspond to one of the integer types. See for example https://docs.microsoft.com/en-us/cpp/c-language/c-bit-fields?view=msvc-170 https://docs.microsoft.com/en-us/cpp/c-language/c-bit-fields....
- chrisseaton 5y agoYeah they compile to the same machine code operations though. If those machine code operations aren't right as a bitfield then they aren't going to be right done manually either. https://godbolt.org/z/csvTx89EG https://godbolt.org/z/csvTx89EG
- layer8 5y agoBit field support being buggy exactly means that they don’t compile to the same machine code as the bit shifting/masking code you would write by hand (if your hand-written code is correct).
- minipci1321 5y ago> Yeah they compile to the same machine code operations though. Not always. Switch your example to AARCH64 and check out the BFI instruction.
- alcover 5y agoIs it not rather void bar(struct y *s, unsigned int foo) { s->c = (s->c & 0xf0) | foo; }
- tom_ 5y agoARM is little-endian, and by tradition bitfield bit indexes are assigned from least significant (bit 0 in ARM terms) to more significant. b occupies bits 4-7 inclusive.