C++:大型无符号整数的编译器警告
我有以下数组,我需要在位图上手动操作。
const unsigned int BITS[32] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536, 131072,
262144, 524288, 1048576, 2097152, 4194304,
8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648};
不幸的是,编译时我得到
警告:此十进制常量仅在 ISO C90 中无符号
我如何删除它?
I have following array, that I need to operate by hand on bitmaps.
const unsigned int BITS[32] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536, 131072,
262144, 524288, 1048576, 2097152, 4194304,
8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648};
Unfortunately, when compiled I get
warning: this decimal constant is unsigned only in ISO C90
How can I remove this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,C 中的整数文字的类型为“signed int”(编辑:但请参阅注释以了解注意事项)。最后一个数字太大,无法表示为有符号 32 位整数,因此您需要通过添加后缀“U”来告诉编译器它是一个无符号整数,如下所示:
请注意,您还可以添加后缀“L”使其成为“long”,但在许多系统上它仍然是 32 位,因此无关紧要。
此外,还有一种更不容易出错(并且更易于阅读)的方法来编写此代码,即使用位移运算符:
或者,如果您出于某种原因不喜欢位移,则以十六进制编写:
Integer literals in C are, by default, of type "signed int" (edit: but see comments for caveats). The last number there is too large to be represented as a signed 32-bit integer, and so you need to tell the compiler that it's an unsigned integer by suffixing it with "U", as:
Note that you can also add a suffix of "L" to make it a "long", but on many systems that's still 32-bit and thus irrelevant.
Also, there's a much less error-prone (and easier-to-read) way to write this code, with the bit-shift operator:
Or, writing in hexadecimal, if you don't like bit shifts for some reason:
您的常量也必须指定为无符号,使用
(UL = Unsigned Long),或者简单地
指定为无符号整数。
正如您现在的代码所示,一个常量(默认情况下是一个普通的
int
,因此是有符号的)被分配给一个unsigned int
,这会生成您的警告。Your constant has to be specified as unsigned too, use
(UL = Unsigned Long), or simply
for an unsigned int.
As your code is right now, a constant, which is by default a plain
int
, and hence signed, is assigned to anunsigned int
, which generates your warning.无需将数据类型定义为无符号正义类型;
否则只需将十进制值转换为十六进制...
Not necessary to define data type as unsigned just type;
else just convert decimal value to hex...