区分大小端的位操作

引言

       大小端一直是一个比较让人头疼的问题,尤其是当其和位运算相结合的时候。在工作当中,我们项目的正式的上板环境是大端环境;ft却是小端环境。而项目中的消息触发机制偏偏又需要进行大量的位操作。为此,我们结合宏来进行函数封装,使接口可以比较自由的使用。(定义了EFT宏的是ft环境,正式上板环境没有定义这个宏)

正文

       下面是共用接口的bit_tool.h的截取

#ifdef _EFT_
/* FT:small end */
#define GET_BITS(type, number, pos, len) (((type)(number) << (pos)) >> (GET_TYPE_BIT_LEN(type) - (len)))
#define CLEAR_BITS(type, number, pos, len) \
    ((number) &= ~((GET_BITS(type, ((type)-1), pos, len)) << (GET_TYPE_BIT_LEN(type) - 1 - (pos))))
#define SET_BITS(type, number, pos, len, field) \
do { \
    CLEAR_BITS(type, number, pos, len); \
    (number) |= (((type)(field) << (GET_TYPE_BIT_LEN(type) - (len))) >> (pos)); \
}while(0)
#else
/* OSS:big end */
#define GET_BITS(type, number, pos, len)  (((type)(number) << (GET_TYPE_BIT_LEN(type) - (pos) - (len))) >> (GET_TYPE_BIT_LEN(type) - (len)))
#define CLEAR_BITS(type, number, pos, len) \
    ((number) &= ~((GET_BITS(type, ((type)-1), pos, len)) >> (pos)))
#define SET_BITS(type, number, pos, len, field) \
do { \
    CLEAR_BITS(type, number, pos, len); \
    (number) |= ((type)(field) << (pos)); \
}while(0)
#endif

       代码的应用示例

    /*
     *利用位运算来组虚中断消息包
   */

    /*
     * vintMsgPara: 只能使用0-15bit,即一个WORD16
     * bit15:flag, flag为1时,bit0 - 8为业务中断回调入参 -- 9个BIT可用参数
     * bit14 - 9,  event id -- 虚中断号,薄平台需要
     * bit0  - 8,  用户参数
     */

    SET_BITS_32(vintMsgPara, 0,  9, para);
    SET_BITS_32(vintMsgPara, 9,  6, vintNo);
    SET_BITS_32(vintMsgPara, 15, 1, 1);

   /*
     *与上面函数对应的解虚中断消息的cellid字段
   */

    cellGid = GET_BITS_32(vintPara, 0, 9);
    UE_LOG_WRN(g_dwUlMacLogId, "DebugTaskVintPdcchCallBack : cellGid = %d!", cellGid);

你可能感兴趣的:(区分大小端的位操作)