Description
Struct bit fields are useful for expressing serialized data formats that use bit packing. We would need to devise a syntax for this. The C and C++ way is to specify a type that is larger than the bit field, and then qualify the type with the smaller size, like this:
struct S {
I32 a : 4 // a is a 4-bit signed field
U32 b : 4 // b is a 4-bit unsigned field
}
This seems awkward. The only purpose of writing I32
seems to be to specify the I
part; you could have written I8
or I16
or I64
there, and the meaning would be the same. It seems to me it would be more natural to directly specify the signedness and the bit width, like this:
struct {
a: signed 4
b: unsigned 4
}
One question is whether the bit packing would control the in-memory representation, or just the serialized form. If it controls the in-memory representation, then how do we implement the bit packing? Using C++ bit packing? Or by code-generating the bit shifts? If it doesn't control the in-memory representation, then the implementation could choose a primitive integer type to hold each bit string. For example, the struct above could be implemented with a pair of U8
fields. That could be more efficient for some operations, but it would waste memory.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status