You cannot determine the size of bit-fields in C. You can, however, find out the size in bits of other types by using the value of CHAR_BIT
, found in <limits.h>
. The size in bits is simply CHAR_BIT * sizeof(type)
.
Do not assume that a C byte is an octet, it is at least 8 bit. There are actual machines with 16 or even 32 bit bytes.
Concerning your edit:
I would say a bit-field int a: n;
has a size of n bits by definition. The extra padding bits when put in a struct belong to the struct and not to the bit-field.
My advice: Don’t use bit-fields but use (arrays of) unsigned char
and work with bitmasks. That way a lot of behaviour (overflow, no padding) is well defined.