Search code examples
cstructbit-fields

Getting negative value with bit-fields


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


Solution

  • If you're working with bitfields, you should use unsigned int. signed int is a problem for bit-fields.