1 | #include <stdint.h> |
2 | |
3 | struct Bits { |
4 | uint32_t : 1; |
5 | uint32_t b1 : 1; |
6 | uint32_t b2 : 2; |
7 | uint32_t : 2; |
8 | uint32_t b3 : 3; |
9 | // Unnamed bitfield (this will get removed). |
10 | uint32_t : 2; |
11 | uint32_t b4 __attribute__ ((aligned(16))); |
12 | uint32_t b5 : 5; |
13 | uint32_t b6 : 6; |
14 | uint32_t b7 : 7; |
15 | uint32_t four : 4; |
16 | }; |
17 | |
18 | struct MoreBits { |
19 | uint32_t a : 3; |
20 | uint8_t : 1; |
21 | uint8_t b : 1; |
22 | uint8_t c : 1; |
23 | uint8_t d : 1; |
24 | }; |
25 | struct MoreBits more_bits; |
26 | |
27 | struct ManySingleBits { |
28 | uint16_t b1 : 1, b2 : 1, b3 : 1, b4 : 1, b5 : 1, b6 : 1, b7 : 1, b8 : 1, |
29 | b9 : 1, b10 : 1, b11 : 1, b12 : 1, b13 : 1, b14 : 1, b15 : 1, b16 : 1, |
30 | b17 : 1; |
31 | }; |
32 | struct ManySingleBits many_single_bits; |
33 | |
34 | struct LargePackedBits { |
35 | uint64_t a : 36; |
36 | uint64_t b : 36; |
37 | } __attribute__((packed)); |
38 | |
39 | #pragma pack(1) |
40 | struct PackedBits { |
41 | char a; |
42 | uint32_t b : 5, c : 27; |
43 | }; |
44 | #pragma pack() |
45 | |
46 | int main(int argc, char const *argv[]) { |
47 | struct Bits bits; |
48 | bits.b1 = 1; |
49 | bits.b2 = 3; |
50 | bits.b3 = 7; |
51 | bits.b4 = 15; |
52 | bits.b5 = 31; |
53 | bits.b6 = 63; |
54 | bits.b7 = 127; |
55 | bits.four = 15; |
56 | |
57 | more_bits.a = 3; |
58 | more_bits.b = 0; |
59 | more_bits.c = 1; |
60 | more_bits.d = 0; |
61 | |
62 | many_single_bits.b1 = 1; |
63 | many_single_bits.b5 = 1; |
64 | many_single_bits.b7 = 1; |
65 | many_single_bits.b13 = 1; |
66 | |
67 | struct PackedBits packed; |
68 | packed.a = 'a'; |
69 | packed.b = 10; |
70 | packed.c = 0x7112233; |
71 | |
72 | struct LargePackedBits large_packed = |
73 | (struct LargePackedBits){0xcbbbbaaaa, 0xdffffeeee}; |
74 | struct LargePackedBits *large_packed_ptr = &large_packed; |
75 | |
76 | return 0; // break here |
77 | } |
78 | |