So I know that
struct bit
{
unsigned char a : 1;
}
will still take up a byte because of padding, my question is this:
struct bit
{
unsigned char a : 1;
...
unsigned char h : 1;
}
Will this struct take up the same size as a char? And if so, am I better off just using a char
instead? I'm asking because I want to use bits as a key, but I'd rather avoid bitwise operations if possible.
There is no guarantees of anything when using bit-fields. That struct can have any size and any bit order. In fact unsigned char
type for bit-fields is not even supported by the standard! Your code is already relying on non-standard extensions.
Forget about bit-fields, forget about char
. You should use uint8_t
and bitwise operators if you want predictable, portable code.