C语言 c中的圆形移位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13289397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular shift in c
提问by user1809300
How does the following code work and what do the variables mean:
以下代码如何工作以及变量的含义是什么:
y = (x << shift) | (x >> (sizeof(x)*CHAR_BIT - shift));
I found in a circular shift article but with no explanation on how this works.
我在一篇循环班次文章中找到了,但没有解释它是如何工作的。
采纳答案by thumbmunkeys
CHAR_BITis the number of bits per byte, should be 8 always.
CHAR_BIT是每字节的位数,应始终为 8。
shiftis the number of bits you want to shift left in a circular fashion, so the bits that get shifted out left, come back on the right.
shift是您要以循环方式向左移动的位数,因此向左移出的位将返回到右侧。
1110 0000 << 2 results in:
1000 0011
code for the example:
示例代码:
y = (x << 2) | (x >> (8 - 2));
回答by Dietrich Epp
This is a method of doing a circular shift. Suppose that xis 8 bits.
这是一种进行循环移位的方法。假设x是 8 位。
+----+----+----+----+----+----+----+----+ | x1 x2 x3 x4 x5 x6 x7 x8 | +----+----+----+----+----+----+----+----+
Then, shifting it left by 3 gives us:
然后,将它左移 3 给我们:
+----+----+----+----+----+----+----+----+ | x4 x5 x6 x7 x8 0 0 0 | +----+----+----+----+----+----+----+----+
Now, CHAR_BIT*sizeof(x)is the same as the width of xin bits, 8. So shifting xto the right by 8 - 3gives us:
现在,CHAR_BIT*sizeof(x)与xin 位的宽度相同,8。所以x向右移动by8 - 3给我们:
+----+----+----+----+----+----+----+----+ | 0 0 0 0 0 x1 x2 x3 | +----+----+----+----+----+----+----+----+
And taking the OR you get:
并采取 OR 你得到:
+----+----+----+----+----+----+----+----+ | x4 x5 x6 x7 x8 x1 x2 x3 | +----+----+----+----+----+----+----+----+
This is technically non-portable because it is non-portable to shift by an amount equal to the width of the type -- so if shift is 8, then the left shift is wrong, and if the shift is 0, then the right shift is wrong. However, this works in practice on all three common behaviors when shifting by the type width. (In practice, the shift amount is reduced by some modulo -- either the bit width of the type or some larger number.)
这在技术上是不可移植的,因为它是不可移植的,移位的量等于类型的宽度——所以如果 shift 是 8,那么左移是错误的,如果 shift 是 0,那么右移是错的。但是,当按类型宽度移动时,这实际上适用于所有三种常见行为。(实际上,移位量会减少一些模数——类型的位宽或一些更大的数字。)
It is called a circular shift or "rotation" because the bits that get shifted out on the left get shifted back in on the right.
它被称为循环移位或“旋转”,因为在左侧移出的位在右侧移回。
Sophisticated compilers will actually compile the code down to a hardware rotation instruction.
复杂的编译器实际上会将代码编译为硬件旋转指令。
回答by Anirudh Ramanathan
(x << shift)
Shifts it 'shift' number of bits to the left, returns the shifted out bits
将其向左“移位”位数,返回移出的位
(x >> (sizeof(x)*CHAR_BIT - shift));
Makes space for accommodating those bits
为容纳这些位腾出空间
CHAR_BITis the number of bits in char, so is 8 mostly.
In C, you don't handle one bit at a time, but at a minimum, char number of bits. So that is the granularity you get.
CHAR_BIT是 char 中的位数,因此主要是 8。在 C 中,您不是一次处理一位,而是至少处理一个字符的位数。这就是您获得的粒度。
In general,
一般来说,
For a char, when you do a bit-rotate, you would do it on an 8-bit field (1 byte)
For an int, when you do a rotate, you would do it on a 32-bit field (4 bytes)
对于字符,当您进行位旋转时,您将在 8 位字段(1 字节)上进行
对于 int,当您进行轮换时,您将在 32 位字段(4 个字节)上进行
Example with 8 bits:
8 位示例:
x = 11010101
shift = 2
x << (shift) = 01010100 //shifted left by 2 bits
= x >> ((1 * CHAR_BIT) - shift)
= x >> (6)
= 00000011 //shifted right by 6bits
ORthese bit-wise to give
OR这些位明智地给予
01010100 //x << 2
00000011 //x >> 6
________
01010111
That is the circular shifted value by 2 bits
即循环移位 2 位的值
回答by Dmytro Uhnichenko
This works with unsigned types only. In the case with a signed negative number most left bits will be substituted by the value of most significant bit (with 1-s) by the right-shift operator (">>")
这仅适用于无符号类型。在有符号负数的情况下,最左边的位将被右移运算符(“>>”)的最高有效位(1-s)的值替换
I'd write it like this:
我会这样写:
y = (x << shift) | ( (x >> (sizeof(x)*CHAR_BIT - shift)) & (0x7F >> (sizeof(x)*CHAR_BIT - shift) );
In here before "|" operator we do confirm that first n bits ( n = sizeof(x)*CHAR_BIT - shift) are zeroed. We also assume, that x is short (2-bytes long). So, it's also type-dependent.
在此处“|”之前 运算符我们确实确认前 n 位( n = sizeof(x)*CHAR_BIT - shift)为零。我们还假设 x 很短(2 字节长)。所以,它也是依赖于类型的。

