【问题标题】:Why am I getting random results with the Shift Operator (C++)?为什么我使用 Shift Operator (C++) 得到随机结果?
【发布时间】:2017-01-24 02:03:35
【问题描述】:

在我一直在编辑的代码中,以前的程序员使用移位运算符将一个中等大的数字添加到 size_t 整数。当我出于调试目的使用这个特定整数时,我发现更改数字并没有产生可预测的结果。

输入:

std::size_t
    foo1 = 100000 << 20,
    foo2 = 200000 << 20,
    foo3 = 300000 << 20,
    foo4 = 400000 << 20;
std::cout << "foos1-4:";
std::cout << foo1;
std::cout << foo2;
std::cout << foo3;
std::cout << foo4;

产量:

foos1-4:
1778384896
18446744072971354112
1040187392
18446744072233156608

我知道这是某种溢出错误,但(据我所知有限) size_t 不应该有这些。据我了解 size_t 是一种无符号整数类型,能够容纳几乎无限数量的整数。

根据我对位移运算符的了解,此代码应将数字乘以 2^20 (1048576)。本站其他页面的链接: What are bitwise shift (bit-shift) operators and how do they work?

注意 - 我手工计算出 foo1 似乎是一个 32 位二进制截断的溢出错误,但所有其他对我来说似乎完全随机。

来自http://en.cppreference.com/w/cpp/types/size_t: std::size_t 可以存储理论上可能的任何类型(包括数组)对象的最大大小。据此,我认为问题必须在于整数的声明方式或位移的操作方式。

发生了什么事?

【问题讨论】:

标签: c++ bit-shift binary-operators


【解决方案1】:

问题不是 std::size_t 而是 int 使用的文字。您可以使用UL 后缀使它们足够长,如下所示:

#include <iostream>

int main()
{
std::size_t
    foo1 = 100000UL << 20,
    foo2 = 200000UL << 20,
    foo3 = 300000UL << 20,
    foo4 = 400000UL << 20;
std::cout << "foos1-4:" << std::endl;
std::cout << foo1 << std::endl;
std::cout << foo2 << std::endl;
std::cout << foo3 << std::endl;
std::cout << foo4 << std::endl;
}

输出:

foos1-4:
104857600000
209715200000
314572800000
419430400000

Live Demo


另请注意,编译器会就此给出警告:

main.cpp:6:19: warning: result of '(100000 << 20)' requires 38 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo1 = 100000 << 20,
            ~~~~~~~^~~~~
main.cpp:7:19: warning: result of '(200000 << 20)' requires 39 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo2 = 200000 << 20,
            ~~~~~~~^~~~~
main.cpp:8:19: warning: result of '(300000 << 20)' requires 40 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo3 = 300000 << 20,
            ~~~~~~~^~~~~
main.cpp:9:19: warning: result of '(400000 << 20)' requires 40 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo4 = 400000 << 20;
            ~~~~~~~^~~~~

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多