C++:大型无符号整数的编译器警告

发布于 2024-08-23 15:24:55 字数 541 浏览 11 评论 0原文

我有以下数组,我需要在位图上手动操作。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

口干舌燥 2024-08-30 15:24:55

默认情况下,C 中的整数文字的类型为“signed int”(编辑:但请参阅注释以了解注意事项)。最后一个数字太大,无法表示为有符号 32 位整数,因此您需要通过添加后缀“U”来告诉编译器它是一个无符号整数,如下所示:

2147483648U

请注意,您还可以添加后缀“L”使其成为“long”,但在许多系统上它仍然是 32 位,因此无关紧要。

此外,还有一种更不容易出错(并且更易于阅读)的方法来编写此代码,即使用位移运算符:

const unsigned int BITS[32] = {1U, 1U<<1, 1U<<2, 1U<<3, 1U<<4,
                               /* and so on */
                               1U<<31};

或者,如果您出于某种原因不喜欢位移,则以十六进制编写:

const unsigned int BITS[32] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20,
                               /* and so on */
                               0x80000000U};

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:

2147483648U

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:

const unsigned int BITS[32] = {1U, 1U<<1, 1U<<2, 1U<<3, 1U<<4,
                               /* and so on */
                               1U<<31};

Or, writing in hexadecimal, if you don't like bit shifts for some reason:

const unsigned int BITS[32] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20,
                               /* and so on */
                               0x80000000U};
惜醉颜 2024-08-30 15:24:55

您的常量也必须指定为无符号,使用

2147483648UL

(UL = Unsigned Long),或者简单地

2147483648U

指定为无符号整数。

正如您现在的代码所示,一个常量(默认情况下是一个普通的 int ,因此是有符号的)被分配给一个 unsigned int ,这会生成您的警告。

Your constant has to be specified as unsigned too, use

2147483648UL

(UL = Unsigned Long), or simply

2147483648U

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 an unsigned int, which generates your warning.

残花月 2024-08-30 15:24:55

无需将数据类型定义为无符号正义类型;

int variable_name=2147483648U;

否则只需将十进制值转换为十六进制...

Not necessary to define data type as unsigned just type;

int variable_name=2147483648U;

else just convert decimal value to hex...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文