I have 8 bool
variables, and I want to "merge" them into a byte.
Is there an easy/preferred method to do this?
How about the other way around, decoding a byte into 8 separate boolean values?
I come in assuming it's not an unreasonable question, but since I couldn't find relevant documentation via Google, it's probably another one of those "nonono all your intuition is wrong" cases.
Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.
Because it's fast. A 32-bit processor typically works with 32-bit values. Working with smaller values involves longer instructions, or extra logic.
bool The bool type takes one byte and stores a value of true (1) or false(0).
A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed. you can't store a variable of size less than 1 byte.
The hard way:
unsigned char ToByte(bool b[8]) { unsigned char c = 0; for (int i=0; i < 8; ++i) if (b[i]) c |= 1 << i; return c; }
And:
void FromByte(unsigned char c, bool b[8]) { for (int i=0; i < 8; ++i) b[i] = (c & (1<<i)) != 0; }
Or the cool way:
struct Bits { unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; }; union CBits { Bits bits; unsigned char byte; };
Then you can assign to one member of the union and read from another. But note that the order of the bits in Bits
is implementation defined.
Note that reading one union member after writing another is well-defined in ISO C99, and as an extension in several major C++ implementations (including MSVC and GNU-compatible C++ compilers), but is Undefined Behaviour in ISO C++. memcpy
or C++20 std::bit_cast
are the safe ways to type-pun in portable C++.
(Also, the bit-order of bitfields within a char
is implementation defined, as is possible padding between bitfield members.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With