在C ++中是文字0xffffffff int还是unsigned [英] Is the literal 0xffffffff int or unsigned in C++

查看:292
本文介绍了在C ++中是文字0xffffffff int还是unsigned的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个,没有类型后缀的整数文字总是 INT 秒。但是,gcc和clang都解释 0xffffffff (或除了使用 - 之外显式设置符号位的任何文字)作为未签名的。哪个是对的? (根据,编译器是)

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)

推荐答案

根据C ++ 11标准的第2.14.2 / 2段,

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


整数文字的类型是表6中相应列表的第一个,其值可以是
表示。

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:


  • INT ;或(如果不适合)

  • unsigned int ;或(如果不合适)

  • long int ;或(如果不适合)

  • unsigned long int ;或(如果不合适)

  • long long int ;或

  • unsigned long long int

  • 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.

假设您的实现具有32位 int ,因为 0xffffffff 不适合 int ,其类型应为 unsigned int 。对于具有64位 int 的实现,类型将是 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 ;或(如果不合适)

  • long int ;或(如果不合适)

  • long long int

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

这篇关于在C ++中是文字0xffffffff int还是unsigned的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆