按位移位(左移或右移)有什么作用以及它的用途是什么?

发布于 2024-11-16 01:56:03 字数 242 浏览 3 评论 0原文

我在我看过的各种代码中看到了运算符 >><< (我实际上都不理解),但是我'我只是想知道它们实际上是做什么的以及它们的实际用途是什么。

如果移位类似于 x * 2x / 2,那么与实际使用 */< 的真正区别是什么/代码> 运算符?有性能差异吗?

I've seen the operators >> and << in various code that I've looked at (none of which I actually understood), but I'm just wondering what they actually do and what some practical uses of them are.

If the shifts are like x * 2 and x / 2, what is the real difference from actually using the * and / operators? Is there a performance difference?

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

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

发布评论

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

评论(9

心意如水 2024-11-23 01:56:03

这是一个小程序,您可以在其中执行一些位操作,包括移位。

你有一个位的集合,并且你将其中一些移到了它们的范围之外:

1111 1110 << 2
1111 1000

它从右侧填充了新的零。 :)

0001 1111 >> 3
0000 0011

从左边开始填充。一种特殊情况是前导 1。它通常表示负值 - 取决于语言和数据类型。通常情况下,如果向右移动,第一位保持原样。

1100 1100 >> 1
1110 0110

它在多次移位中是保守的:

1100 1100 >> 2
1111 0011

如果你不想保留第一位,你可以使用(据我所知,在 Java、Scala、C++、C 中,也许更多)一个三重符号运算符

1100 1100 >>> 1
0110 0110

:在另一个方向上没有任何等价物,因为它没有任何意义 - 也许在你非常特殊的背景下,但在一般情况下没有意义。

从数学上讲,左移一次是 *=2,左移 2 次是 *=4,依此类推。右移是 a /= 2 等等。

Here is an applet where you can exercise some bit-operations, including shifting.

You have a collection of bits, and you move some of them beyond their bounds:

1111 1110 << 2
1111 1000

It is filled from the right with fresh zeros. :)

0001 1111 >> 3
0000 0011

Filled from the left. A special case is the leading 1. It often indicates a negative value - depending on the language and datatype. So often it is wanted, that if you shift right, the first bit stays as it is.

1100 1100 >> 1
1110 0110

And it is conserved over multiple shifts:

1100 1100 >> 2
1111 0011

If you don't want the first bit to be preserved, you use (in Java, Scala, C++, C as far as I know, and maybe more) a triple-sign-operator:

1100 1100 >>> 1
0110 0110

There isn't any equivalent in the other direction, because it doesn't make any sense - maybe in your very special context, but not in general.

Mathematically, a left-shift is a *=2, 2 left-shifts is a *=4 and so on. A right-shift is a /= 2 and so on.

卷耳 2024-11-23 01:56:03

左移位乘以 2 的任意次方,右移位则除以 2 的任意次方。

例如,x = x * 2; 也可以写成 x<<1x = x*8 可以写成x<<3(因为 2 的 3 次方是 8)。类似地,x = x / 2;x>>1 等等。

Left bit shifting to multiply by any power of two and right bit shifting to divide by any power of two.

For example, x = x * 2; can also be written as x<<1 or x = x*8 can be written as x<<3 (since 2 to the power of 3 is 8). Similarly x = x / 2; is x>>1 and so on.

人海汹涌 2024-11-23 01:56:03

左移

x = x * 2^value(正常操作)

x << value(按位运算)


x = x * 16(与2^4相同)

左移等效项为x = x << 4

右移

x = x / 2^value(普通算术运算)

x >>> value(按位运算)


x = x / 8(与2^3相同)

右移等效项为x =x>> 3

Left Shift

x = x * 2^value (normal operation)

x << value (bit-wise operation)


x = x * 16 (which is the same as 2^4)

The left shift equivalent would be x = x << 4

Right Shift

x = x / 2^value (normal arithmetic operation)

x >> value (bit-wise operation)


x = x / 8 (which is the same as 2^3)

The right shift equivalent would be x = x >> 3

傲影 2024-11-23 01:56:03

左移:等于要移位的值与2的要移位的位数次方的乘积。

示例:

1 << 3
0000 0001  ---> 1
Shift by 1 bit
0000 0010 ----> 2 which is equal to 1*2^1
Shift By 2 bits
0000 0100 ----> 4 which is equal to 1*2^2
Shift by 3 bits
0000 1000 ----> 8 which is equal to 1*2^3

右移:它等于必须移位 2 的值的商,该商是要移位的位数次方。

例子:

8 >> 3
0000 1000  ---> 8 which is equal to 8/2^0
Shift by 1 bit
0000 0100 ----> 4 which is equal to 8/2^1
Shift By 2 bits
0000 0010 ----> 2 which is equal to 8/2^2
Shift by 3 bits
0000 0001 ----> 1 which is equal to 8/2^3

Left shift: It is equal to the product of the value which has to be shifted and 2 raised to the power of number of bits to be shifted.

Example:

1 << 3
0000 0001  ---> 1
Shift by 1 bit
0000 0010 ----> 2 which is equal to 1*2^1
Shift By 2 bits
0000 0100 ----> 4 which is equal to 1*2^2
Shift by 3 bits
0000 1000 ----> 8 which is equal to 1*2^3

Right shift: It is equal to quotient of value which has to be shifted by 2 raised to the power of number of bits to be shifted.

Example:

8 >> 3
0000 1000  ---> 8 which is equal to 8/2^0
Shift by 1 bit
0000 0100 ----> 4 which is equal to 8/2^1
Shift By 2 bits
0000 0010 ----> 2 which is equal to 8/2^2
Shift by 3 bits
0000 0001 ----> 1 which is equal to 8/2^3
兔小萌 2024-11-23 01:56:03

左移位以乘以 2 的任意幂。
右移位以除以 2 的任意幂。

x = x << 5; // Left shift
y = y >> 5; // Right shift

在 C/C++ 中可以写成:

#include <math.h>

x = x * pow(2, 5);
y = y / pow(2, 5);

Left bit shifting to multiply by any power of two.
Right bit shifting to divide by any power of two.

x = x << 5; // Left shift
y = y >> 5; // Right shift

In C/C++ it can be written as,

#include <math.h>

x = x * pow(2, 5);
y = y / pow(2, 5);
烟织青萝梦 2024-11-23 01:56:03

/* 运算符相比,移位运算符的效率更高。

在计算机体系结构中,除法(/)或乘法(*)需要多个时间单元和寄存器来计算结果,而移位运算符只是一个寄存器和一个时间单元计算。

The bit shift operators are more efficient as compared to the / or * operators.

In computer architecture, divide(/) or multiply(*) take more than one time unit and register to compute result, while, bit shift operator, is just one one register and one time unit computation.

森末i 2024-11-23 01:56:03

一些示例:

  • 位操作,例如与 Base64 之间的转换(6 位而不是 8 位) )
  • 执行 2 次方运算(1 << 4 等于 2^4 即 16)
  • 在使用位时编写更可读的代码。例如,使用定义常量
    <代码>1 << 4 或 1 << 5 更具可读性。

Some examples:

  • Bit operations for example converting to and from Base64 (which is 6 bits instead of 8)
  • doing power of 2 operations (1 << 4 equal to 2^4 i.e. 16)
  • Writing more readable code when working with bits. For example, defining constants using
    1 << 4 or 1 << 5 is more readable.
自此以后,行同陌路 2024-11-23 01:56:03

是的,我认为在性能方面您可能会发现差异,因为可以使用巨大的数据集以 o(1) 的复杂度执行按位左移和右移操作。

例如,计算 2 ^ n 的幂:

int value = 1;
while (exponent<n)
    {
       // Print out current power of 2
        value = value *2; // Equivalent machine level left shift bit wise operation
        exponent++;
         }
    }

使用按位左移运算的类似代码如下所示:

value = 1 << n;

此外,执行按位运算就像精确复制用户级数学运算(这是处理的最终机器级指令)由微控制器和处理器)。

Yes, I think performance-wise you might find a difference as bitwise left and right shift operations can be performed with a complexity of o(1) with a huge data set.

For example, calculating the power of 2 ^ n:

int value = 1;
while (exponent<n)
    {
       // Print out current power of 2
        value = value *2; // Equivalent machine level left shift bit wise operation
        exponent++;
         }
    }

Similar code with a bitwise left shift operation would be like:

value = 1 << n;

Moreover, performing a bit-wise operation is like exacting a replica of user level mathematical operations (which is the final machine level instructions processed by the microcontroller and processor).

雨夜星沙 2024-11-23 01:56:03

这是一个例子:

#include"stdio.h"
#include"conio.h"

void main()
{
    int rm, vivek;
    clrscr();
    printf("Enter any numbers\t(E.g., 1, 2, 5");
    scanf("%d", &rm); // rm = 5(0101) << 2 (two step add zero's), so the value is 10100
    printf("This left shift value%d=%d", rm, rm<<4);
    printf("This right shift value%d=%d", rm, rm>>2);
    getch();
}

Here is an example:

#include"stdio.h"
#include"conio.h"

void main()
{
    int rm, vivek;
    clrscr();
    printf("Enter any numbers\t(E.g., 1, 2, 5");
    scanf("%d", &rm); // rm = 5(0101) << 2 (two step add zero's), so the value is 10100
    printf("This left shift value%d=%d", rm, rm<<4);
    printf("This right shift value%d=%d", rm, rm>>2);
    getch();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文