Skip to content

Commit 640cd97

Browse files
committed
Explore the result of behaviour which cause overflow
1 parent 3538007 commit 640cd97

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

c/BitField/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
int main()
3+
{
4+
struct BitField
5+
{
6+
unsigned int a : 3;
7+
int b : 2;
8+
unsigned int c : 1;
9+
};
10+
struct BitField bitfield;
11+
bitfield.a = 125; // bitfield.a = x -> while(bitfield.a is not in the range) -> bitfield.a = num +- (2^3)
12+
bitfield.b = -5; // bitfield.b = x -> while(bitfield.b is not in the range) -> bitfield.b = num +- (2^2)
13+
bitfield.c = -1;
14+
printf("%d\n%d\n%d\n", bitfield.a, bitfield.b, bitfield.c);
15+
printf("sizeof(struct BitField) is %llu\n", sizeof(struct BitField));
16+
17+
printf("125%(2^3)=%d\n", 125 % (1 << 3));
18+
printf("(-5)%(2^2)=%d\n", -5 % (1 << 2));
19+
}

0 commit comments

Comments
 (0)