<input id="0qass"><u id="0qass"></u></input>
  • <input id="0qass"><u id="0qass"></u></input>
  • <menu id="0qass"><u id="0qass"></u></menu>

    分享兩個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語言表驅動法編程實踐(精華帖,建議收藏并實踐)

    覺得本次分享的文章對您有幫助,隨手點[在看]并轉發分享,也是對我的支持。

    Engineer-Bruce_Yang CSDN認證博客專家 嵌入式硬件 單片機 arm開發
    本科畢業于華南理工大學,現美國卡羅爾工商管理碩士研究生在讀,曾就職于世界名企偉易達、聯發科技等,多年嵌入式產品開發經驗,在智能玩具、安防產品、平板電腦、手機開發有豐富的實戰開發經驗,現任深圳市云之手科技有限公司副總經理、研發總工程師。
    ??2020 CSDN 皮膚主題: Age of Ai 設計師:meimeiellie 返回首頁
    多乐彩