File tree Expand file tree Collapse file tree 2 files changed +44
-8
lines changed Expand file tree Collapse file tree 2 files changed +44
-8
lines changed Original file line number Diff line number Diff line change 1
1
# c_bitfields_integer_promotion
2
- Little experiment to check how integer promotion affects bitfields
3
2
4
- The interest in this came after the discussion at
https://lore.kernel.org/all/[email protected] /t/#u
3
+ Little experiment to check how integer promotion affects bitfields.
4
+
5
+ The interest in this came after the discussion at
< https://lore.kernel.org/all/[email protected] /t/#u >
6
+
7
+ Turns out that for bitfields bigger than ` sizeof(int) ` the behavior is compiler dependent.
5
8
6
9
## A bunch of related links
7
10
8
- https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
11
+ < https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html >
9
12
10
- https://stackoverflow.com/questions/2647320/struct-bitfield-max-size-c99-c
13
+ < https://stackoverflow.com/questions/2647320/struct-bitfield-max-size-c99-c >
11
14
12
- https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Fields
15
+ < https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Fields >
13
16
14
- https://archive.org/details/the-ansi-c-programming-language-by-brian-w.-kernighan-dennis-m.-ritchie.org/page/132/mode/2up
17
+ < https://archive.org/details/the-ansi-c-programming-language-by-brian-w.-kernighan-dennis-m.-ritchie.org/page/132/mode/2up >
15
18
16
- https://stackoverflow.com/questions/32529080/should-bit-fields-less-than-int-in-size-be-the-subject-of-integral-promotion
19
+ < https://stackoverflow.com/questions/32529080/should-bit-fields-less-than-int-in-size-be-the-subject-of-integral-promotion >
17
20
18
- https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules
21
+ < https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules >
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+
3
+ struct s1 {
4
+ unsigned int a : 20 ;
5
+ unsigned int _ : 12 ;
6
+ };
7
+
8
+ struct s2 {
9
+ unsigned long long b : 40 ;
10
+ unsigned long long _ : 24 ;
11
+ };
12
+
13
+ int main ()
14
+ {
15
+ struct s1 s1 ;
16
+ struct s2 s2 ;
17
+
18
+ s1 .a = 2 ;
19
+ s2 .b = 2 ;
20
+
21
+ unsigned int a = s1 .a << 19 ;
22
+ unsigned long long b = s2 .b << 39 ;
23
+
24
+ printf ("0x%x\n" , a ); //both gcc and clang: 0x100000
25
+ printf ("0x%llx\n" , b ); //gcc: 0x0 clang: 0x10000000000
26
+
27
+ return 0 ;
28
+ }
29
+
30
+ /*
31
+ Debian clang version 18.1.8
32
+ gcc (Debian 12.2.0-14) 12.2.0
33
+ */
You can’t perform that action at this time.
0 commit comments