Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the literal 0xffffffff int or unsigned in C++

Tags:

According to this, integer literals without type suffix are always ints. However, both gcc and clang interpret 0xffffffff (or any literal which explicitly sets the sign bit other than using the -) as unsigned. Which is correct? (according to this the compilers are)

like image 731
Walter Avatar asked Apr 01 '13 13:04

Walter


1 Answers

Per Paragraph 2.14.2/2 of the C++11 Standard,

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

Table 6 reports that for hexadecimal constants, the type should be:

  • int; or (if it doesn't fit)
  • unsigned int; or (if it doesn't fit)
  • long int; or (if it doesn't fit)
  • unsigned long int; or (if it doesn't fit)
  • long long int; or
  • unsigned long long int.

Assuming your implementation has 32-bit int, since 0xffffffff does not fit in an int, its type should be unsigned int. For an implementation with a 64-bit int, the type would be int.

Notice, that if you had written the same literal as a decimal constant instead, the type could have only been:

  • int; or (if it doesn't fit)
  • long int; or (if it doesn't fit)
  • long long int.
like image 139
Andy Prowl Avatar answered Oct 19 '22 00:10

Andy Prowl