问题
I have a question related to bit-fields in C. Here I have such a structure:
struct Register
{
int bit:1;
};
int main(void)
{
struct Register bit = {1};
printf("\nbit = %d", bit.bit);
return 0;
}
Can you please explain me why do I get:
bit = -1
回答1:
If you're working with bitfields, you should use unsigned int
. signed int
is a problem for bit-fields.
回答2:
use unsigned int , it stores 0 and 1,
struct Register
{
unsigned int bit:1;
};
int main(void)
{
struct Register bit = {1};
printf("\nbit = %d", bit.bit);
return 0;
}
来源:https://stackoverflow.com/questions/30614340/getting-negative-value-with-bit-fields