分享两个C库源码中的移位函数
扫描二维码
随时随地手机看文章
以下源代码是之前阅读C库代码获取的,分享一下:
-
_lrotr()将一个无符号长整形数左循环移位的函数 -
原形:unsigned long _lrotr(unsigned long value,int count) -
功能:将value向左循环移动count位。 -
返回值:将value向左循环移动count位后的值。 -
头文件:stdlib.h
unsigned _rotl (unsigned val,int shift)
{
register unsigned hibit; /* non-zero means hi bit set */
register unsigned num = val; /* number to rotate */
shift &= 0x1f; /* modulo 32 -- this will also make
negative shifts work */
while (shift--) {
hibit = num & 0x80000000; /* get high bit */
num <<= 1; /* shift left one bit */
if (hibit)
num |= 1; /* set lo bit if hi bit was set */
}
return num;
}
//这个函数就是将_rotl函数做再次封装
unsigned long _lrotl (unsigned long val,int shift)
{
return( (unsigned long) _rotl((unsigned) val, shift) );
}
-
_lrotr()将一个无符号长整形数右循环移位的函数 -
原形:unsigned long _lrotr(unsigned long value,int count) -
功能:将value向右循环移动count位。 -
返回值:将value向右循环移动count位后的值。 -
头文件:stdlib.h
unsigned _rotr (unsigned val,int shift)
{
register unsigned lobit; /* non-zero means lo bit set */
register unsigned num = val; /* number to rotate */
shift &= 0x1f; /* modulo 32 -- this will also make
negative shifts work */
while (shift--) {
lobit = num & 1; /* get high bit */
num >>= 1; /* shift right one bit */
if (lobit)
num |= 0x80000000; /* set hi bit if lo bit was set */
}
return num;
}
//对右移函数做再次封装
unsigned long _lrotr (unsigned long val,int shift)
{
return( (unsigned long) _rotr((unsigned) val, shift) );
}
简单对以上两个核心函数进行测试,测试环境基于DevC++:
#include <stdio.h>
unsigned _rotl (unsigned val,int shift)
{
register unsigned hibit; /* non-zero means hi bit set */
register unsigned num = val; /* number to rotate */
shift &= 0x1f; /* modulo 32 -- this will also make
negative shifts work */
while (shift--) {
hibit = num & 0x80000000; /* get high bit */
num <<= 1; /* shift left one bit */
if (hibit)
num |= 1; /* set lo bit if hi bit was set */
}
return num;
}
//这个函数就是将_rotl函数做再次封装
unsigned long _lrotl (unsigned long val,int shift)
{
return( (unsigned long) _rotl((unsigned) val, shift) );
}
unsigned _rotr (unsigned val,int shift)
{
register unsigned lobit; /* non-zero means lo bit set */
register unsigned num = val; /* number to rotate */
shift &= 0x1f; /* modulo 32 -- this will also make
negative shifts work */
while (shift--) {
lobit = num & 1; /* get high bit */
num >>= 1; /* shift right one bit */
if (lobit)
num |= 0x80000000; /* set hi bit if lo bit was set */
}
return num;
}
//对右移函数做再次封装
unsigned long _lrotr (unsigned long val,int shift)
{
return( (unsigned long) _rotr((unsigned) val, shift) );
}
int main(void)
{
unsigned long val = 2;
unsigned long ret = _lrotl(val , 4) ;
printf("%d\n",ret);
unsigned long r = _lrotr(ret,4);
printf("%d\n",r);
return 0 ;
}
运行结果:
往期精彩
C语言常用的几种排序
C语言、嵌入式中几个非常实用的宏技巧
C语言#和##连接符在项目中的应用(漂亮)
C语言表驱动法编程实践(精华帖,建议收藏并实践)
觉得本次分享的文章对您有帮助,随手点[在看]
并转发分享,也是对我的支持。
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!