[HN Gopher] C Bit-Field Pitfalls
___________________________________________________________________
C Bit-Field Pitfalls
Author : fanf2
Score : 25 points
Date : 2026-03-21 18:42 UTC (4 hours ago)
(HTM) web link (www.os2museum.com)
(TXT) w3m dump (www.os2museum.com)
| mwkaufma wrote:
| tl;dr standard is unclear if they should respect the signed-ness
| of the declaration (MSCV), or always promote to int before
| converting to a receiving type (GCC, Clang).
|
| I suppose you could say MS's choice reflects a commitment to
| backwards compatibility, whereas GCC/Clang is always chomping at
| the bit to introduce more aggressive optimizations that signed-
| integer-undefined-behavior affords?
| pjmlp wrote:
| It is much safer to pack/unpack bits manually than trusting
| bitfields will work as expected.
| fsckboy wrote:
| > _The troublesome behavior is demonstrated by the lines
| performing the left shift. We take a 12-bit wide bit-field, shift
| it left by 20 bits so ..._
|
| this is nonsense. I don't know what they expect would happen, but
| who cares? I wouldn't shift a 12 bit field by more than +-11
| bits.
|
| you can shift the "enclosing" word of memory if you want, just
| put the original definition in a union.
___________________________________________________________________
(page generated 2026-03-21 23:01 UTC)