Signed bit field represetation

You should never use plain int as the bitfield type if you’re expecting something about the value besides that it can hold n bits – according to the C11 standard it is actually implementation-defined whether int in a bit-field is signed or unsigned 6.7.2p5: 5 Each of the comma-separated multisets designates the same type, except … Read more

Questions about C bitfields

Because a and c are not contiguous, they each reserve a full int’s worth of memory space. If you move a and c together, the size of the struct becomes 8 bytes. Moreover, you are telling the compiler that you want a to occupy only 1 bit, not 1 byte. So even though a and … Read more

How to simulate bit-fields in Delphi records?

Thanks everyone! Based on this information, I reduced this to : RBits = record public BaseMid: BYTE; private Flags: WORD; function GetBits(const aIndex: Integer): Integer; procedure SetBits(const aIndex: Integer; const aValue: Integer); public BaseHi: BYTE; property _Type: Integer index $0005 read GetBits write SetBits; // 5 bits at offset 0 property Dpl: Integer index $0502 … Read more

When is it worthwhile to use bit fields?

Bit-fields are typically only used when there’s a need to map structure fields to specific bit slices, where some hardware will be interpreting the raw bits. An example might be assembling an IP packet header. I can’t see a compelling reason for an emulator to model a register using bit-fields, as it’s never going to … 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