I wrote the following and I expected that 16
would be printed.
#include <iostream>
enum E : long { e = 16 };
struct X
{
E e : 5;
};
X x;
int main(){ x.e = E::e; std::cout << static_cast<int>(x.e) << std::endl; }
But it wasn't. I got a compiler warning and -16
was printed instead. The warning was:
warning: implicit truncation from 'E' to bitfield changes value from 16 to -16
It's unclear to me. Why was the warning display and why was -16
printed? I declared the bit-field of size of 5
that's enough to store 16
in there.