<< C++ 中的运算符?

发布于 2024-11-03 12:26:12 字数 129 浏览 4 评论 0 原文

我是 C++ 新手,下面语句中 << 的确切含义是什么,谢谢。

if (Val & (0x0001 << 0))
{}
else
{}

I am new to C++, what's the exact meaning for the << in statement below, Thanks.

if (Val & (0x0001 << 0))
{}
else
{}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

柠檬色的秋千 2024-11-10 12:26:12

这是左移操作。如果:

a << b

其中 ab 是整型(char、short、long 等),则 a 中的位会移位左边的 b 位置,右边填充零。换句话说,a 乘以 2^b

示例:

12 << 3

12 (decimal) = 00001100 (binary)

左移 3 位:

00001100 becomes 01100000 

即 96(即 12 * 812 * 2^3

It is a shift-left operation. If you have:

a << b

where a and b are integral types (char, short, long, etc.), then the bits in a are shifted left b places with zeroes filling in on the right. In other words, a is multiplied by 2^b.

Example:

12 << 3

12 (decimal) = 00001100 (binary)

shift left 3 places:

00001100 becomes 01100000 

which is 96 (which is 12 * 8 or 12 * 2^3)

才能让你更想念 2024-11-10 12:26:12

意思是把0x0001号0位左移。在这种特定情况下,它什么也不做。

例如,如果是 (0x0001 << 4),则 0x0001 将变为 0x0010。每左移一个位置就相当于将数字乘以 2。

It means shift 0x0001 number 0 bits to the left. In that specific case, it does nothing.

For example, if it was (0x0001 << 4), 0x0001 would become 0x0010. Each position shifted left is like multiplying the number by 2.

旧时模样 2024-11-10 12:26:12

这是一个位移运算符

但是,当不涉及整数时,请注意底层 重载运算符

That is a bit shift operator.

But when integers aren't involved, beware of an underlying overloaded operator.

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