C++ >> 是什么意思?做
在这种情况下>>会做什么?
int n = 500;
unsigned int max = n>>4;
cout << max;
它打印出31。
它对500做了什么才能使其达到31?
What does >> do in this situation?
int n = 500;
unsigned int max = n>>4;
cout << max;
It prints out 31.
What did it do to 500 to get it to 31?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有点移位了!
500 的原始二进制:
111110100
已转移 4
000011111 即 31!
这相当于整数除以 16。500
/16 = 31
500/2^4 = 31
从这里提取的一些事实: http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitshift.html(因为我脑子里胡言乱语会导致毫无成效的胡言乱语……这些人说得更清楚比我能)
使用 << 向左移动导致 0 从最低有效端(右侧)移位,并导致位从最高有效端(左侧)脱落。
使用 >> 右移如果数字无符号,则导致 0 从最高有效端(左侧)移位,并导致位从最低有效端(右侧)丢失。
位移位不会改变被移位的变量的值。相反,会使用位移后的结果创建一个临时值。
Bit shifted!
Original binary of 500:
111110100
Shifted 4
000011111 which is 31!
This is equivilent of doing integer division by 16.
500/16 = 31
500/2^4 = 31
Some facts pulled from here: http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitshift.html (because blarging from my head results in rambling that is unproductive..these folks state it much cleaner than i could)
Shifting left using << causes 0's to be shifted from the least significant end (the right side), and causes bits to fall off from the most significant end (the left side).
Shifting right using >> causes 0's to be shifted from the most significant end (the left side), and causes bits to fall off from the least significant end (the right side) if the number is unsigned.
Bitshifting doesn't change the value of the variable being shifted. Instead, a temporary value is created with the bitshifted result.
500 向右移位了 4 次。
x>> y
在数学上意味着x / 2^y
。因此
500 / 2^4
等于500 / 16
。整数除法的结果是31
。500 got bit shifted to the right 4 times.
x >> y
mathematically meansx / 2^y
.Hence
500 / 2^4
which is equal to500 / 16
. In integer division the result is31
.它使用整数除法将 500 除以 16。
>>
是右移运算符,它将n
的二进制表示形式向右移动 4 次。这相当于将n
除以2 4 次,即除以2^4=16。这是整数除法,因此小数部分被截断。It divided 500 by 16 using integer division.
>>
is a right-shift operator, which shifted the bits of the binary representation ofn
to the right 4 times. This is equivalent to dividingn
by 2 4 times, i. e. dividing it by 2^4=16. This is integer division, so the decimal part got truncated.它将 500 的位向右移动 4 位位置,同时丢弃最右边的位。
500 = 111110100(二进制)
111110100>> 4 = 11111 = 31
It shifts the bits of 500 to the right by 4 bit positions, tossing out the rightmost bits as it does so.
500 = 111110100 (binary)
111110100 >> 4 = 11111 = 31
111110100 是二进制的 500。将这些位向右移动,剩下 11111,即二进制的 31。
111110100 is 500 in binary. Move the bits to the right and you are left with 11111 which is 31 in binary.
500 二进制为
[1 1111 0100]
(4 + 16 + 32 + 64 + 128 + 256)
将其右移 4 次,您将丢失最低 4 位,结果是:
[1 1111]
,即1 + 2 + 4 + 8 + 16 = 31
您还可以用十六进制检查它:< /strong>
500
(十进制)为0x1F4
(十六进制)。然后向右移动 4 位,或一个半字节:
0x1F
==31
(十进制)。500 in binary is
[1 1111 0100]
(4 + 16 + 32 + 64 + 128 + 256)
Shift that to the right 4 times and you lose the lowest 4 bits, resulting in:
[1 1111]
which is 1 + 2 + 4 + 8 + 16 = 31
You can also examine it in Hex:
500
(decimal) is0x1F4
(hex).Then shift to the right 4 bits, or one nibble:
0x1F
==31
(dec).>>和<<运营商正在转变运营商。
http://www-numi.fnal.gov/ Offline_software/srt_public_context/WebDocs/Companion/cxx_crib/shift.html
当然,它们可能会超载,只是为了让您更加困惑!
The >> and << operators are shifting operators.
http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/shift.html
Of course they may be overloaded just to confuse you a little more!
C++ 有很好的类动画位级别发生的事情
C++ has nice classes to animate what is going on at the bit level