我正在致力于仅使用按位运算符在 C 中创建逻辑右移函数。这是我所拥有的:
int logical_right_shift(int x, int n)
{
int size = sizeof(int); // size of int
// arithmetic shifts to create logical shift, return 1 for true
return (x >> n) & ~(((x >> (size << 3) - 1) << (size << 3) -1)) >> (n-1);
}
这实际上适用于除 n = 0 之外的所有情况。我一直在尝试找出一种方法来修复它,以便它也适用于 n = 0,但我被困住了。
int lsr(int x, int n)
{
return (int)((unsigned int)x >> n);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)