c ++在移动<< 32次后打印出不正确的二进制文件
c++ printing out incorrect binary after shifting << 32 times
下面的代码应该打印出 32 个 0,然后是 011,然后是 0,直到从右到左的 64 位
#include <iostream>
#include <bitset>
#include <stdint.h>
using namespace std;
int main() {
int a = 0b011;
long long b = (a << 32);
std::cout << bitset<64>(b).to_string();
}
但它给出了这个:
main.cpp:8:20: warning: shift count >= width of type [-Wshift-count-overflow]
long long b = (a << 32);
^ ~~
1 warning generated.
错误的输入是这样的:
0000000000000000000000000000000000000000010000000000101011110000
似乎移位是在 32 位中完成的,因为 a
和 32
都是 32 位长。
转换为 long long
,然后再进行修复。
long long b = ((long long)a << 32);
下面的代码应该打印出 32 个 0,然后是 011,然后是 0,直到从右到左的 64 位
#include <iostream>
#include <bitset>
#include <stdint.h>
using namespace std;
int main() {
int a = 0b011;
long long b = (a << 32);
std::cout << bitset<64>(b).to_string();
}
但它给出了这个:
main.cpp:8:20: warning: shift count >= width of type [-Wshift-count-overflow]
long long b = (a << 32);
^ ~~
1 warning generated.
错误的输入是这样的:
0000000000000000000000000000000000000000010000000000101011110000
似乎移位是在 32 位中完成的,因为 a
和 32
都是 32 位长。
转换为 long long
,然后再进行修复。
long long b = ((long long)a << 32);