Trying to write an overlength value to a bitfield:
#include <stdio.h>
struct test
{
char a:1;
char b:1;
} test1;
int main() {
printf("%d\n", sizeof(struct test));
printf("%.2x\n", *(char *)&test1);
test1.a = 2;
test1.b = 1;
printf("%.2x\n", *(char *)&test1);
return 0;
}
The result is:
/tmp/fo0ZfcmKnf.c: In function 'main':
/tmp/fo0ZfcmKnf.c:14:11: warning: overflow in conversion from 'int' to 'signed char:1' changes value from '2' to '0' [-Woverflow]
14 | test1.a = 2;
| ^
/tmp/fo0ZfcmKnf.o
1
00
02
The write operation to bitfield "a" is lost as the LSB of number "2" is 0b. Therefore, when writing an oversized number to a bitfield, only the equivalent length of bits of the bitfield will be written. Just like writing 0xFFF to an unsigned char.