How does a C-style struct with a bitfield get represented in a Rust #[repr(C)] struct?

No, you can’t. There is an open issue about supporting bitfields, which doesn’t seem to be active. In the issue, @retep998 explains how bitfields are handled in winapi. That might be helpful if you need to handle bitfields in C interface. OP seems to aim at C interoperation, but if you just need bitfields functionality, … Read more

Practical Use of Zero-Length Bitfields

You use a zero-length bitfield as a hacky way to get your compiler to lay out a structure to match some external requirement, be it another compiler’s or architecture’s notion of the layout (cross-platform data structures, such as in a binary file format) or a bit-level standard’s requirements (network packets or instruction opcodes). A real-world … Read more

Bit fields in C#

I’d probably knock together something using attributes, then a conversion class to convert suitably attributed structures to the bitfield primitives. Something like… using System; namespace BitfieldTest { [global::System.AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] sealed class BitfieldLengthAttribute : Attribute { uint length; public BitfieldLengthAttribute(uint length) { this.length = length; } public uint Length { get { return length; … Read more

tech