I played with https://gcc.godbolt.org/ and this code seems to always return 1.
However I wonder if standard guarantee this.
#include <cstdint>
struct S{
uint8_t a : 6;
uint8_t b : 2;
};
int main(){
return sizeof(S);
}
My real example is following:
struct Pair{
uint64_t created; // 8 bytes
uint32_t expires; // 4 bytes
uint16_t keylen; // 2 bytes, 4 bits are used for vallen. 2 bits are reserved for future versions. 10 bytes for keylen.
uint16_t vallen; // 2 bytes
};
Currently I do some bitmasks and shifts.
Can I do it like this? Will it always work (any "normal" compiler)?
How keylen
behave on Big Endian? Using shifts I always use first 4 bits.
struct Pair{
uint64_t created;
uint32_t expires;
uint8_t vallen2 : 4;
uint8_t future : 2;
uint16_t keylen : 10;
uint16_t vallen;
};