C 中任意整数的位循环
将整数 2 传递给此函数,然后返回整数 4
x = 2;
x = rotateInt('L', x, 1);
(将位左移 1)
示例: 00000010->向左旋转 1 -> 00000100
但如果我通过这个:
x = rotateInt('R', x, 3);
它将返回 64, 01000000
这是代码,有人可以纠正错误...谢谢
int rotateInt(char direction, unsigned int x, int y)
{
unsigned int mask = 0;
int num = 0, result = 0;
int i;
for (i = 0; i < y; i++)
{
if (direction == 'R')
{
if ((x & 1) == 1)
x = (x ^ 129);
else
x = x >> 1;
}
else if (direction == 'L')
{
if ((x & 128) == 1)
x = (x ^ 129);
else
x = x << 1;
}
}
result = (result ^ x);
return result;
}
Pass a integer 2 to this function and then return a integer which is 4
x = 2;
x = rotateInt('L', x, 1);
(left shift the bits by 1)
Example:
00000010 -> rotate left by 1 -> 00000100
but if I pass this:
x = rotateInt('R', x, 3);
it will return 64, 01000000
Here is the code, can someone correct the error... thanks
int rotateInt(char direction, unsigned int x, int y)
{
unsigned int mask = 0;
int num = 0, result = 0;
int i;
for (i = 0; i < y; i++)
{
if (direction == 'R')
{
if ((x & 1) == 1)
x = (x ^ 129);
else
x = x >> 1;
}
else if (direction == 'L')
{
if ((x & 128) == 1)
x = (x ^ 129);
else
x = x << 1;
}
}
result = (result ^ x);
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
所以,我假设您知道什么是右移和左移。并且您知道算术移位和逻辑移位之间的区别。
C 只有算术移位。它不进行逻辑移位,也不进行旋转。好吧,我撒谎了,C 对无符号整数进行逻辑移位。
旋转确实是这样的:它与逻辑移位相同,只不过当您移位超过数字末尾时,数字“环绕”到另一侧。例如,
0010
右旋转为0001
。如果再次右旋转,您将得到1000
。看,1
环绕或旋转到整数的另一侧。左旋转类似:
0100
左旋转1000
左旋转0001
左旋转0010
等。请注意,旋转不不像算术右移那样保留符号位。
所以,C 语言只有算术移位。所以你必须手动实现“旋转”部分。所以,向左旋转。您可能想要:
您应该能够找出类似的右旋转方法。
祝你好运!
So, I'll assume you know what right and left shifts are. And that you know the difference between arithmetic and logical shifts.
C only has arithmetic shifts. It doesn't do logical shifts, nor does it do rotates. okay I lied, C does logical shifts on unsigned ints.
A rotate does, well, exactly that: it's the same as a logical shift, except when you shift past the end of the number, the digits "wrap around" to the other side. For example
0010
right-rotated is0001
. If you right-rotate again, you get1000
. See, the1
wrapped around, or rotated, to the other side of the integer.The left rotate is similar:
0100
left rotate1000
left rotate0001
left rotate0010
etc.Note that rotates don't keep the sign bit as an arithmetic right-shift would.
So, C only has arithmetic shifts. So you have to implement the "rotate" part manually. So, take a left-rotate. You would want to:
You should be able to figure out a similar method for right rotates.
good luck!
接受的答案非常好而且直接。
然而,我正在做一些 K&R 练习来刷新我的 C,并想分享这个向右旋转的函数,这对于尝试学习按位运算的人来说可能会派上用场。
对于左旋转,只需将 -1 到 -31 之间的值传递给
bitAmount
参数即可。请注意,此功能更注重可教性/易读性/简单性,而不是效率/可移植性/紧凑性。
The accepted answer is very nice and straight-forward.
However, I was doing some K&R exercises to refresh my C, and wanted to share this rotate-to-the-right function which may come in handy for people trying to learn bit-wise operations.
For left-rotations just pass values between -1 and -31 to the
bitAmount
argument.Do note that this function favors teachability/legibility/simplicity over efficiency/portability/compactness.
由于没有人告诉您如何实现这一点,您可以使用内在函数,对于 Visual Studio,它们是 _rotl、_rotl64、_rotr、_rotr64。
哦,但是旋转和移位是两个不同的东西!
Since no one told you how to implement this, you can use intrinsics, for visual studio they are _rotl, _rotl64, _rotr, _rotr64.
Oh, but rotation and shifts are 2 different things!
我也在阅读 K&R,所以这是一个非常简单的无符号整数旋转函数:
其中 n 是要旋转的位数。
I was reading K&R, too, so this is a very straightforward rotate function for unsigned int:
Where
n
is the number of bits to rotate.查看按位移位运算符:
http://en.wikipedia。 org/wiki/Bitwise_operators#Shifts_in_C.2C_C.2B.2B_and_Java
Have a look at the bitwise shift operators:
http://en.wikipedia.org/wiki/Bitwise_operators#Shifts_in_C.2C_C.2B.2B_and_Java
看来你向右旋转是正确的。 1从侧面摔倒,又从左边折回来?
不管怎样,这里是你的成分:
http://tigcc.ticalc.org/doc/keywords。 html#if - 用于确定它是“L”还是“R”
http ://tigcc.ticalc.org/doc/keywords.html#for - 用于计算移位次数
和
http://msdn.microsoft.com/en-us/library/f96c63ed(VS.80).aspx - 实际移动它
,玩它。它最终会起作用的!
It seems like your rotate to the right is RIGHT. 1 fell of the side and returned back again from the left?
Anyway, here are your ingredients:
http://tigcc.ticalc.org/doc/keywords.html#if - for determining if it is 'L' or 'R'
http://tigcc.ticalc.org/doc/keywords.html#for - for counting number of times to shift
and
http://msdn.microsoft.com/en-us/library/f96c63ed(VS.80).aspx - to actually shift it
Go, play with it. It WILL work eventually!
我建议使用
unsigned int
。I recommend using an
unsigned int
.