以下源代碼是之前閱讀C庫代碼獲取的,分享一下:
-
_lrotr()將一個無符號長整形數(shù)左循環(huán)移位的函數(shù) -
原形:unsigned long _lrotr(unsigned long value,int count) -
功能:將value向左循環(huán)移動count位。 -
返回值:將value向左循環(huán)移動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;
}
//這個函數(shù)就是將_rotl函數(shù)做再次封裝
unsigned long _lrotl (unsigned long val,int shift)
{
return( (unsigned long) _rotl((unsigned) val, shift) );
}
-
_lrotr()將一個無符號長整形數(shù)右循環(huán)移位的函數(shù) -
原形:unsigned long _lrotr(unsigned long value,int count) -
功能:將value向右循環(huán)移動count位。 -
返回值:將value向右循環(huán)移動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;
}
//對右移函數(shù)做再次封裝
unsigned long _lrotr (unsigned long val,int shift)
{
return( (unsigned long) _rotr((unsigned) val, shift) );
}
簡單對以上兩個核心函數(shù)進行測試,測試環(huán)境基于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;
}
//這個函數(shù)就是將_rotl函數(shù)做再次封裝
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;
}
//對右移函數(shù)做再次封裝
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 ;
}
運行結(jié)果:
往期精彩
C語言常用的幾種排序
C語言、嵌入式中幾個非常實用的宏技巧
C語言#和##連接符在項目中的應(yīng)用(漂亮)
C語言表驅(qū)動法編程實踐(精華帖,建議收藏并實踐)
覺得本次分享的文章對您有幫助,隨手點[在看]
并轉(zhuǎn)發(fā)分享,也是對我的支持。
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!