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

Related questions

2 answers 242 views
242 views asked Jan 5, 2016 by avibootz
1 answer 134 views
1 answer 126 views
126 views asked May 10, 2022 by avibootz
1 answer 215 views
215 views asked Aug 28, 2016 by avibootz
1 answer 120 views
3 answers 261 views
...