Question about bit ordering for boolean array elements #20
-
|
I've been expanding on serde-beve recently and I've discovered my handling of boolean arrays is inconsistent and flawed. One important thing I realize isn't clear in the spec is the ordering of bits in the byte. Is the first boolean at the least significant bit, or the most significant bit? In other words, is it right-to-left or left-to-right? Left-to-right example: I think it's lsb-first, but it isn't made explicit in the spec (unless i'm overlooking something) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Thanks for the feedback. The Boolean array packing does need to be explained more explicitly. I'll try to update the spec soon, but here's some example C++ for the packing: const auto num_bytes = (value.size() + 7) / 8;
std::vector<uint8_t> bytes(num_bytes);
for (size_t byte_i{}, i{}; byte_i < num_bytes; ++byte_i) {
for (size_t bit_i = 0; bit_i < 8 && i < value.size(); ++bit_i, ++i) {
bytes[byte_i] |= uint8_t(value[i]) << uint8_t(bit_i);
}
}So, for each byte, bit 0 is the LSB (index 0). 0b00000001 would be true in the 0 index of the bit array. 0b00000010 would be true in the 1 index of the array. Bits are then padded to the nearest multiple of 8. Think of the packing as byte specific, where each progressive byte packs the next 8 bits in LSB zero index ordering. |
Beta Was this translation helpful? Give feedback.
Thanks for the feedback. The Boolean array packing does need to be explained more explicitly. I'll try to update the spec soon, but here's some example C++ for the packing:
So, for each byte, bit 0 is the LSB (index 0). 0b00000001 would be true in the 0 index of the bit array. 0b00000010 would be true in the 1 index of the array.
Bits are then padded to th…