c语言 位段操作

Modified on: Mon, 17 Jun 2019 23:31:50 +0800 热度: 957 度
#include <stdio.h>
typedef struct{
    unsigned int flag1 :3;
    unsigned int flag2 :2;
    unsigned int trailing : 27;
}A;

void showbin(unsigned int num)
{
    unsigned int mask = 1u << 31;
    do
    {
        if(mask&0x808080) putchar(' ');
        printf("%d",num & mask ?1:0);
    }
    while(mask>>=1);
}
int main(int argc, const char * argv[]) {
    A a;
    a.flag1=2;
    a.flag2=3;
    a.trailing=0;
    printf("%lu\n",sizeof(a));
    showbin(*(int*)&a);
    putchar(10);
    return 0;
}

添加新评论