devxlogo

Declaring a bit field for a variable

Declaring a bit field for a variable

Question:
Is there a way to explicitly declare the number of bits to allocate to a specific variable? For example, can I declare an integer that would be stored in nine bits?

Answer:
Here is the syntax to define bitfields in C/C++:

struct bitField{   int a:1; // a has one bit  int b:7; // b has seven bits  // .. and so on};

devx-admin

Share the Post: