引言
? ? ? ?大小端一直是一個(gè)比較讓人頭疼的問題,尤其是當(dāng)其和位運(yùn)算相結(jié)合的時(shí)候。在工作當(dāng)中,我們項(xiàng)目的正式的上板環(huán)境是大端環(huán)境;ft卻是小端環(huán)境。而項(xiàng)目中的消息觸發(fā)機(jī)制偏偏又需要進(jìn)行大量的位操作。為此,我們結(jié)合宏來進(jìn)行函數(shù)封裝,使接口可以比較自由的使用。(定義了EFT宏的是ft環(huán)境,正式上板環(huán)境沒有定義這個(gè)宏)
正文
? ? ? ?下面是共用接口的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
? ? ? ?代碼的應(yīng)用示例
/*
*利用位運(yùn)算來組虛中斷消息包
*/
/*
* vintMsgPara: 只能使用0-15bit,即一個(gè)WORD16
* bit15:flag, flag為1時(shí),bit0 - 8為業(yè)務(wù)中斷回調(diào)入?yún)?-- 9個(gè)BIT可用參數(shù)
* bit14 - 9, event id -- 虛中斷號,薄平臺需要
* bit0 - 8, 用戶參數(shù)
*/
SET_BITS_32(vintMsgPara, 0, 9, para);
SET_BITS_32(vintMsgPara, 9, 6, vintNo);
SET_BITS_32(vintMsgPara, 15, 1, 1);
/*
*與上面函數(shù)對應(yīng)的解虛中斷消息的cellid字段
*/
cellGid = GET_BITS_32(vintPara, 0, 9);
UE_LOG_WRN(g_dwUlMacLogId, "DebugTaskVintPdcchCallBack : cellGid = %d!", cellGid);