Re: Strange enum type conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux