以下源代码是之前阅读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 ; }
运行结果: