bit segment
- Combines the bits of an int into a structure
struct {
unsigned int leading:3;
unsigned int FLAG1:1;
unsigned int FLAG2:1;
int trailing:11;
};
#include<stdio.h>
void ptrBin(unsigned int number);
struct U0{
unsigned int leading:3;
unsigned int FLAG1:1;
unsigned int FLAG2:1;
int trailing:27;
};
int main(){
struct U0 uu;
uu.leading = 2;
uu.FLAG1 = 0;
uu.FLAG2 = 1;
uu.trailing = 0;
printf("sizeof(uu)=%lu\n",sizeof(uu));
ptrBin(*(int*)&uu);
return 0;
}
void ptrBin(unsigned int number){
unsigned mask = 1u << 31;
for(;mask;mask >>=1){
printf("%d",number & mask? 1:0);
}
printf("\n");
}
sizeof(uu)=4
00000000000000000000000000010010
- Can be accessed directly by the member name of the bit field
- More convenient than shift, AND, or
- The compiler arranges the ordering of the bits in it, which is not portable
- When more than one int is required, multiple ints are used