How to define and use 3 bit fields in C

1 Answer

0 votes
#include <stdio.h>

struct S 
{
    unsigned int b : 3; // three-bit values are 0..7
};

int main(void)
{
    struct S s = {6};
    
    printf("%d\n", s.b); 
    ++s.b; 
    printf("%d\n", s.b); 
    ++s.b; // overflow
    printf("%d\n", s.b); 
        
    return 0;
}

/*
run:
 
6
7
0

*/

 



answered Aug 28, 2016 by avibootz
...