C位域代码

// Online C compiler to run C program online
#include 

typedef struct bits
{
    unsigned char b0 : 1;
    unsigned char b1 : 1;
    unsigned char b2 : 1;
    unsigned char b3 : 1;
    
    unsigned char b4 : 1;
    unsigned char b5 : 1;
    unsigned char b6 : 1;
    unsigned char b7 : 1;
}BIT, *pBIT;

int main() {
    
    BIT bit_field;
    pBIT p_bit_field = &bit_field;
    
    p_bit_field->b0 = 1;
    p_bit_field->b1 = 1;
    p_bit_field->b2 = 1;
    p_bit_field->b3 = 1;
    p_bit_field->b4 = 1;
    p_bit_field->b5 = 1;
    p_bit_field->b6 = 1;
    p_bit_field->b7 = 1;
    
    if(p_bit_field->b0 & 1)
    {
        printf("bit0 is set\n");     
    }
    if(p_bit_field->b1 & 1)
    {
        printf("bit1 is set\n");     
    }
    if(p_bit_field->b2 & 1)
    {
        printf("bit2 is set\n");     
    }
    if(p_bit_field->b3 & 1)
    {
        printf("bit3 is set\n");    
    }
    if(p_bit_field->b4 & 1)
    {
        printf("bit4 is set\n");     
    }
    if(p_bit_field->b5 & 1)
    {
        printf("bit5 is set\n");     
    }
    if(p_bit_field->b6 & 1)
    {
        printf("bit6 is set\n");     
    }
    if(p_bit_field->b7 & 1)
    {
        printf("bit7 is set\n");     
    }

    return 0;
}

你可能感兴趣的:(C,c语言,开发语言)