简体   繁体   English

偏移1001某个值会得到与预期不同的结果

[英]Shifting 1001 certain value gives a different result than expected

I am doing the following. 我正在做以下事情。 I would like to shift 1001 to the left 我想向左移动1001

int a = 0;
a = (1001 << 2);
std::cout << a; //Gives 4004

I was expecting it to be in binary 100100 which is 36. Why am I getting 0000111110100100 ? 我期望它的二进制值为100100 ,即36。为什么我得到0000111110100100

1001 is a decimal constant. 1001是十进制常数。 All integers literals are decimal unless prefixed accordingly. 除非有相应的前缀,否则所有整数文字都是十进制的。 If you wish to obtain a binary constant, then prefix it with 0b (only available since C++14 though). 如果您希望获得一个二进制常量,请在其0b加上0b (仅自C ++ 14起可用)。

a = (0b1001 << 2);

Otherwise, hexadecimal is a decent alternative: 否则,十六进制是一个不错的选择:

a = (0x9 << 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将值移动到比其大小大的位不会导致0? - Why does shifting a value further than its size not result in 0? 离散傅立叶变换实现给出与OpenCV DFT不同的结果 - Discrete Fourier Transform implementation gives different result than OpenCV DFT OpenCV中的逆FFT与Matlab提供不同的结果 - Inverse FFT in OpenCV gives different result than Matlab 使用对 const 的引用打印变量值会产生不同的结果 - Printing variable value using Reference to const gives different result 为什么Matlab赋予的价值不同于C ++ - Why does Matlab gives different value than C++ cout指向数组项目的指针输出的值与预期值不同 - cout pointer to array item is printing out different value than expected 函数调用后,地址处的c ++值与预期值不同 - c++ value at address is different than expected after function call 计算提供最大值的30位整数的位移数 - Calculate number of bit shifting in 30 bit integers that gives a maximum value boost :: regex_match提供的结果与许多在线reggex测试器不同 - boost::regex_match gives different result than many online reggex tester 为什么一个表达式中的std :: string串联给出的结果与char逐个char的结果不同? - Why std::string concatenation in one expression gives different result than char by char?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM