Take a look at the following code:
#include <stdio.h> struct data{int A: 1 ; int B: 31 ;}; int Main () {struct data test; TEST.A = 1 ; if (test.a = = 1 ) printf ( "a = = 1\n" ); else printf ( "a!=1\n" ); printf ( "a =%d" , TEST.A);}
The structure of this code defines a bit-field variable of type int, want to be used to receive 0 or 1, according to this, after assigning 1 to a, then the result of the program should be "A==1". But actually the result is as follows:
This is because when the 1 is stored in memory, the computer will think this is a negative, negative numbers in memory in the form of complement, the original code is-1.
As we all know, the largest positive number in the 32-bit compiler is the trailing 31 bits are all 1, the highest bit is the sign bit, if all 1 is the complement, the original code is-1.
If int is an unsigned type then there is no problem, declaring an int of the above code as an unsigned type to get the desired result, the result of my program running here is as follows:
C-language structure of postural domain