做64位的有效的方式使用旋转32位值 [英] Efficient way of doing 64 bit rotate using 32 bit values
本文介绍了做64位的有效的方式使用旋转32位值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要旋转使用2 32位寄存器64位值。有没有人碰到这样的一个有效的方式?
I need to rotate a 64 bit value using 2 32 bit registers. Has anyone come across an efficient way of doing this?
推荐答案
好吧,正常的旋转可以这样实现的:
Well, a normal rotate can be implemented like this:
unsigned int rotate(unsigned int bits, unsigned int n) {
return bits << n | (bits >> (32 - n));
}
所以,这里的在64位实现猜测的32位瓦尔:
So, here's a guess at a 64-bit implementation with 32-bit vars:
void bit_rotate_left_64(unsigned int hi, unsigned int lo, unsigned int n,
unsigned int *out_hi, unsigned int *out_lo) {
unsigned int hi_shift, hi_rotated;
unsigned int lo_shift, lo_rotated;
hi_shift = hi << n;
hi_rotated = hi >> (32 - n);
lo_shift = lo << n;
lo_rotated = lo >> (32 - n);
*out_hi = hi_shift | lo_rotated;
*out_lo = lo_shift | hi_rotated;
}
基本上,我只是把旋转位从高字和OR-ING与低位字它们,反之亦然。
Basically, I'm just taking the rotated bits from the high word and OR-ing them with the low word, and vice-versa.
下面是一个简单的测试:
Here's a quick test:
int main(int argc, char *argv[]) {
/* watch the one move left */
hi = 0;
lo = 1;
for (i = 0; i < 129; i++) {
bit_rotate_left_64(hi, lo, 1, &hi, &lo);
printf("Result: %.8x %.8x\n", hi, lo);
}
/* same as above, but the 0 moves left */
hi = -1U;
lo = 0xFFFFFFFF ^ 1;
for (i = 0; i < 129; i++) {
bit_rotate_left_64(hi, lo, 1, &hi, &lo);
printf("Result: %.8x %.8x\n", hi, lo);
}
}
这篇关于做64位的有效的方式使用旋转32位值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文