Strange enum type conversion
John (Eljay) Love-Jensen
eljay@adobe.com
Thu Dec 3 15:16:00 GMT 2009
Hi Richard,
> wrong. That's an int with a bit pattern that's all 1's. On a two's
> complement machine with 32-bit ints that's equivalent to -1.
On a 1's or 2's complement 32-bit int machine using C++, 0xFFFFFFFF is an
unsigned int.
It's not equivalent to -1, it is equivalent to ~0U.
EXPERIMENT (the comments on the std::cout lines being actual output)
#include <iostream>
#include <typeinfo>
int main()
{
// i is signed int
// j is unsigned int
std::cout << typeid(0).name() << std::endl; // i
std::cout << typeid(0U).name() << std::endl; // j
std::cout << typeid(-1).name() << std::endl; // i
std::cout << typeid(~0).name() << std::endl; // i
std::cout << typeid(~0U).name() << std::endl; // j
std::cout << typeid(0xFFFFFFFF).name() << std::endl; // j
std::cout << typeid(0xFFFFFFFFU).name() << std::endl; // j
}
Note that 0xFFFFFFFF is 'j' (unsigned int), as I expected.
Sincerely,
--Eljay
More information about the Gcc-help
mailing list