如何将字节数组中的所有位向左/向右旋转一定量
I can rotate a word to left or right by a certain amount like this:
#define ROR(x, r) ((x >> r) | (x << (64 - r)))
#define ROL(x, r) ((x << r) | (x >> (64 - r)))
[...]
ROR(var1, 11);
ROL(var1, 11);
How can I do the same but with an entire array of bytes (I mean: all the bits in array sequence)? An array like this:
uint32_t somearray[12] = {
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
};
PS: There is a similar question here, but I需要知道如何用一定的数量进行操作。
I can rotate a word to left or right by a certain amount like this:
#define ROR(x, r) ((x >> r) | (x << (64 - r)))
#define ROL(x, r) ((x << r) | (x >> (64 - r)))
[...]
ROR(var1, 11);
ROL(var1, 11);
How can I do the same but with an entire array of bytes (I mean: all the bits in array sequence)? An array like this:
uint32_t somearray[12] = {
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
};
PS: There is a similar question here, but I need to know how to do it with some amount.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ror
和rol
宏:x
是uint64_t
。UINT64_T
转换为64位置的行为不确定。如果偏移数量r
可以为null,则应修改表达式以避免转移64。这是一个修改版本:
请注意,建议使用内联函数而不是宏来避免具有副作用和强制操作数的操作数问题:
要旋转一个完整的单词,将其他不同的数组用作源更容易和目的地并编写一个循环:
这是一个测试程序:
There are 2 problems with the
ROR
andROL
macros:x
is auint64_t
.uint64_t
by 64 positions has undefined behavior. If the number of shiftsr
can be null, you should modify the expression to avoid shifting by 64.Here is a modified version:
Note that it is recommended to use inline functions instead of macros to avoid problems with operands with side effects and enforce operand sizes:
To rotate a full array of words, it is simpler to use a different array as the source and destination and write a loop:
Here is a test program:
要旋转 array 中的所有位,请创建一个函数,该函数占用大小,指向数据的指针和移位量。
对于32位值的数组,数组的位移动可能与
some_32_bit&gt;&gt; SH
由于endian。与_Generic
对宏的更先进的使用解决。To rotate all the bits in an array, create a function that takes a size, a pointer to the data, and a shift amount.
For an array of 32-bit values, the bit shift of the array may be be the same arithmetically as
some_32_bit >> sh
due to endian. More advanced use of macros with_Generic
solve solve that.