|
|
|
|
|
by petermclean
323 days ago
|
|
I've been working on an open source library that, while it doesn't solve bitfields, can provide convenient ergonomic access to bit-granular types: https://github.com/PeterCDMcLean/BitLib You can inherit from bit_array and create a pseudo bitfield: class mybits_t : bit::bit_array<17> {
public:
// slice a 1 bit sized field
// returns a proxy reference to the single bit
auto field1() {
return (*this)(0, 1);
}
// slice a 3 bit sized field
// returns a proxy reference to the 3 bits
auto field2() {
return (*this)(1, 4);
}
};
mybits_t mybits(7);
assert(mybits.field1() == 1);
mybits.field1() = 0;
assert(static_cast<int>(mybits) == 6); //no implicit cast to integers, but explicit supported
There is minimal overhead depending on how aggressively the compiler inlines or optimizes* |
|