位运算:位段

位段

  • 把一个int的若干位组合成一个结构
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");
}
//result
sizeof(uu)=4
00000000000000000000000000010010

  • 可以直接用位段的成员名称来访问
    • 比移位、与、或还方便
  • 编译器会安排其中的位的排序,不具有可移植性
  • 当所需的位超过一个int是会采用多个int

猜你喜欢

转载自blog.csdn.net/qq_38593446/article/details/80033593