C++ >> 是什么意思?做

发布于 2024-09-16 00:43:07 字数 235 浏览 3 评论 0原文

在这种情况下>>会做什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

哆兒滾 2024-09-23 00:43:07

有点移位了!

500 的原始二进制:
111110100

已转移 4
000011111 即 31!

Original: 111110100
1st Shift:011111010
2nd Shift:001111101
3rd Shift:000111110
4th Shift:000011111 which equals 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!

Original: 111110100
1st Shift:011111010
2nd Shift:001111101
3rd Shift:000111110
4th Shift:000011111 which equals 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.

爱情眠于流年 2024-09-23 00:43:07

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 means x / 2^y.

Hence 500 / 2^4 which is equal to 500 / 16. In integer division the result is 31.

梦幻之岛 2024-09-23 00:43:07

它使用整数除法将 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 of n to the right 4 times. This is equivalent to dividing n by 2 4 times, i. e. dividing it by 2^4=16. This is integer division, so the decimal part got truncated.

濫情▎り 2024-09-23 00:43:07

它将 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

自控 2024-09-23 00:43:07

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.

没有心的人 2024-09-23 00:43:07

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) is 0x1F4(hex).
Then shift to the right 4 bits, or one nibble:
0x1F == 31(dec).

疯了 2024-09-23 00:43:07

>>和<<运营商正在转变运营商。

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!

白色秋天 2024-09-23 00:43:07

C++ 有很好的类动画位级别发生的事情

#include <bitset>
#include <iostream>

int main() {
    std::bitset<16> s(500);
    for(int i = 0; i < 4; i++) {
      std::cout << s << std::endl;
      s >>= 1;
    }

    std::cout << s
              << " (dec " << s.to_ulong() << ")"
              << std::endl;
}

C++ has nice classes to animate what is going on at the bit level

#include <bitset>
#include <iostream>

int main() {
    std::bitset<16> s(500);
    for(int i = 0; i < 4; i++) {
      std::cout << s << std::endl;
      s >>= 1;
    }

    std::cout << s
              << " (dec " << s.to_ulong() << ")"
              << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文