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