[英]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.