简体   繁体   English

在C ++中是文字0xffffffff int还是unsigned

[英]Is the literal 0xffffffff int or unsigned in C++

According to this , integer literals without type suffix are always int s. 根据这个 ,没有类型后缀的整数文字总是int However, both gcc and clang interpret 0xffffffff (or any literal which explicitly sets the sign bit other than using the - ) as unsigned. 但是,gcc和clang都将0xffffffff (或任何明确设置符号位的文字除了使用- )解释为无符号。 Which is correct? 哪个是对的? (according to this the compilers are) (根据这个编译器是)

Per Paragraph 2.14.2/2 of the C++11 Standard, 根据C ++ 11标准的第2.14.2 / 2段,

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented. 整数文字的类型是表6中相应列表的第一个,其中可以表示其值。

Table 6 reports that for hexadecimal constants, the type should be: 表6报告了对于十六进制常量,类型应为:

  • int ; int ; or (if it doesn't fit) 或(如果不合适)
  • unsigned int ; unsigned int ; or (if it doesn't fit) 或(如果不合适)
  • long int ; long int ; or (if it doesn't fit) 或(如果不合适)
  • unsigned long int ; unsigned long int ; or (if it doesn't fit) 或(如果不合适)
  • long long int ; long long int ; or 要么
  • unsigned long long int . 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 . 假设你的实现有32位int ,因为0xffffffff不适合int ,它的类型应该是unsigned int For an implementation with a 64-bit int , the type would be int . 对于具有64位int ,类型将为int

Notice, that if you had written the same literal as a decimal constant instead, the type could have only been: 请注意,如果您将相同的文字写成十进制常量,则该类型可能只有:

  • int ; int ; or (if it doesn't fit) 或(如果不合适)
  • long int ; long int ; or (if it doesn't fit) 或(如果不合适)
  • long long int . long long int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM