c bit access struct

Solutions on MaxInterview for c bit access struct by the best coders in the world

showing results for - "c bit access struct"
Tiago
22 Mar 2019
1typedef union a429_tag { // define a union of structs and/or raw data types
2  // struct to access each bit individually
3  struct {
4    unsigned int
5        bit0  : 1, bit1  : 1, bit2  : 1, bit3  : 1,
6        bit4  : 1, bit5  : 1, bit6  : 1, bit7  : 1,
7        bit8  : 1, bit9  : 1, bit10 : 1, bit11 : 1,
8        bit12 : 1, bit13 : 1, bit14 : 1, bit15 : 1;
9  };
10  // struct to access range of bits by name
11  struct {
12    unsigned int
13        label  : 8,
14        sdi    : 2,
15        data   : 3,
16        ssm    : 2,
17        parity : 1;
18  };
19  // int type to access to the entire word as an integer
20  unsigned int word : 16;
21  
22} a429_type;
23
24/* Example usage */
25int main() {
26  a429_type myUnion;
27  myUnion.parity = 1;
28  if (myUnion.bit15 == 1) {
29    printf("parity and bit15 refer to the same bit");
30  }
31  return 0;
32}