struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
} test;
Why is the :
used instead of =
. I'm really confused.
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
} test;
Why is the :
used instead of =
. I'm really confused.
This is bit fields and it is used to specify that struct members occupies exactly given number of bits.
In your example it is 4 bits for test.a
, 5 for test.b
and 7 bits for test.c
.
This is useful for typecasting for example. You can have short
variable that can be cast to test
and get exactly needed bits.
Read this for more information.