bitfield integer problem

Hey,

Does anyone know why this bitfield integer is -1

1
2
3
4
5
6
7
8
9
int my_int : 1;

my_int = 1;
std::cout << my_int << std::endl;
//here it outputs -1

my_int = 0;
std::cout << my_int << std::endl;
//here it outputs 0 


can anyone help?

Last edited on
Because the two's complement of a 1-bit number with value 1 is -1.
https://en.wikipedia.org/wiki/Two%27s_complement

The possible values that my_int can have are 0 and -1. If you want to store 1 instead of -1 you should declare it as an unsigned integer.

 
unsigned int my_int : 1;


ahh :D
thank you :)
Topic archived. No new replies allowed.