常用的bit位操作
//判断某1位是1还是0
#ifndef GET_BIT
#define BIT_IS_1(value,bitpos) (((value)&(1<<(bitpos)))>>(bitpos))
#endif//读取指定位置bit位的值
#ifndef GET_BIT
#define GET_BIT(value,bitpos) ((value)&(1<<(bitpos)))
#endif//取反指定位置bit位的值
#ifndef REV_BIT
#define REV_BIT(value,bitpos) ((value)^=(1<<(bitpos)))
#endif//把某个bit位置0
#ifndef SET_0_BIT
#define SET_0_BIT(value,bitpos) ((value)&=~(1<<(bitpos)))
#endif//把某个bit位置1
#ifndef SET_1_BIT
#define SET_1_BIT(value,bitpos) ((value)|= (1<<(bitpos)))
#endif//将两个unsigned char 转换成 unsigned short(BYTExBYTE2WORD)
#ifndef MAKE_WORD
#define MAKE_WORD(low, high) ((unsigned short)(((unsigned char)(((unsigned __int64)(low)) & 0xff)) | ((unsigned short)((unsigned char)(((unsigned __int64)(high)) & 0xff))) << 8))
#endif//将两个unsigned short转换成 long(WORDxWORD2DWORD)
#ifndef MAKE_LONG
#define MAKE_LONG(low, high) ((long)(((unsigned short)(((unsigned __int64)(low)) & 0xffff)) | ((unsigned long)((unsigned short)(((unsigned __int64)(high)) & 0xffff))) << 16))
#endif
//取一个数的其中几位,例如:x的bit[7,4]:表示状态,则取值计算为 auto val=GET_4LO2HI(x,4,7);
#ifndef GET_4LO2HI
#ifdef __cplusplus
#define GET_4LO2HI(value,from,to) GetRangValue(value,from,to)
template<typename Type>
inline Type GetRangValue(Type vaule,int from,int to)
{
Type ret=0;
int temp = ((sizeof(Type)*8)-1)-to;
ret = (Type)(vaule<<temp);
ret =ret>>(temp+from);
return ret;
}
#else
/*
*在c语言中不能进行类型推导,故定义了如下几个宏
*/
#define GET_4LO2HI_USHORT(value,low,high) (((unsigned short)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_UCHAE(value,low,high) (((unsigned char)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_UINT(value,low,high) (((unsigned int)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_ULONG(value,low,high) (((unsigned long)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_U_INT64(value,low,high) (((unsigned __int64)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_SHORT(value,low,high) (((signed short)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_CHAE(value,low,high) (((signed char)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_INT(value,low,high) (((signed int)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_LONG(value,low,high) (((signed long)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI__INT64(value,low,high) (((signed __int64)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#endif
#endif
/*如
* 偏移地址 说明
*0x00 bit[7:0],对应 id 高 8 位
* bit[15:8],帧类型,
* 0x04,命令帧;
* 0x01,数据帧;
* 0x05,状态帧;
*0x01 bit[15:0],对应 id 低 16 位
* char _0x00addrValue=0xaa;
* char _0x01addrValue=0x55;
* auto id = MAKE_LONG_BIT(_0x01addrValue,0,15,_0x00addrValue,0,7);
*/
#ifndef MAKE_LONG_BIT
#define MAKE_LONG_BIT(low,lowfrom,lowto,high,highfrom,highto) MAKE_LONG(GET_4LO2HI(low,lowfrom,lowto),GET_4LO2HI(high,highfrom,highto))
#endif//取DWORD的低位WORD
#ifndef LO_WORD
#define LO_WORD(l) ((unsigned short)(((unsigned __int64)(l)) & 0xffff))
#endif//取DWORD的高位WORD
#ifndef HI_WORD
#define HI_WORD(l) ((unsigned short)((((unsigned __int64)(l)) >> 16) & 0xffff))
#endif//取WORD的低位BYTE
#ifndef LO_BYTE
#define LO_BYTE(w) ((unsigned char)(((unsigned __int64)(w)) & 0xff))
#endif//取WORD的高位BYTE
#ifndef HI_BYTE
#define HI_BYTE(w) ((unsigned char)((((unsigned __int64)(w)) >> 8) & 0xff))
#endif
//判断某1位是1还是0
#ifndef GET_BIT
#define BIT_IS_1(value,bitpos) (((value)&(1<<(bitpos)))>>(bitpos))
#endif//读取指定位置bit位的值
#ifndef GET_BIT
#define GET_BIT(value,bitpos) ((value)&(1<<(bitpos)))
#endif//取反指定位置bit位的值
#ifndef REV_BIT
#define REV_BIT(value,bitpos) ((value)^=(1<<(bitpos)))
#endif//把某个bit位置0
#ifndef SET_0_BIT
#define SET_0_BIT(value,bitpos) ((value)&=~(1<<(bitpos)))
#endif//把某个bit位置1
#ifndef SET_1_BIT
#define SET_1_BIT(value,bitpos) ((value)|= (1<<(bitpos)))
#endif//将两个unsigned char 转换成 unsigned short(BYTExBYTE2WORD)
#ifndef MAKE_WORD
#define MAKE_WORD(low, high) ((unsigned short)(((unsigned char)(((unsigned __int64)(low)) & 0xff)) | ((unsigned short)((unsigned char)(((unsigned __int64)(high)) & 0xff))) << 8))
#endif//将两个unsigned short转换成 long(WORDxWORD2DWORD)
#ifndef MAKE_LONG
#define MAKE_LONG(low, high) ((long)(((unsigned short)(((unsigned __int64)(low)) & 0xffff)) | ((unsigned long)((unsigned short)(((unsigned __int64)(high)) & 0xffff))) << 16))
#endif
//取一个数的其中几位,例如:x的bit[7,4]:表示状态,则取值计算为 auto val=GET_4LO2HI(x,4,7);
#ifndef GET_4LO2HI
#ifdef __cplusplus
#define GET_4LO2HI(value,from,to) GetRangValue(value,from,to)
template<typename Type>
inline Type GetRangValue(Type vaule,int from,int to)
{Type ret=0;int temp = ((sizeof(Type)*8)-1)-to;ret = (Type)(vaule<<temp);ret =ret>>(temp+from);return ret;
}
#else
/*
*在c语言中不能进行类型推导,故定义了如下几个宏
*/
#define GET_4LO2HI_USHORT(value,low,high) (((unsigned short)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_UCHAE(value,low,high) (((unsigned char)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_UINT(value,low,high) (((unsigned int)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_ULONG(value,low,high) (((unsigned long)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_U_INT64(value,low,high) (((unsigned __int64)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_SHORT(value,low,high) (((signed short)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_CHAE(value,low,high) (((signed char)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_INT(value,low,high) (((signed int)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI_LONG(value,low,high) (((signed long)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#define GET_4LO2HI__INT64(value,low,high) (((signed __int64)(value<<(((sizeof(value)*8)-1)-high)))>>((((sizeof(value)*8)-1)-high)+low))
#endif
#endif
/*如* 偏移地址 说明*0x00 bit[7:0],对应 id 高 8 位* bit[15:8],帧类型,* 0x04,命令帧;* 0x01,数据帧;* 0x05,状态帧;*0x01 bit[15:0],对应 id 低 16 位* char _0x00addrValue=0xaa;* char _0x01addrValue=0x55;* auto id = MAKE_LONG_BIT(_0x01addrValue,0,15,_0x00addrValue,0,7);
*/
#ifndef MAKE_LONG_BIT
#define MAKE_LONG_BIT(low,lowfrom,lowto,high,highfrom,highto) MAKE_LONG(GET_4LO2HI(low,lowfrom,lowto),GET_4LO2HI(high,highfrom,highto))
#endif//取DWORD的低位WORD
#ifndef LO_WORD
#define LO_WORD(l) ((unsigned short)(((unsigned __int64)(l)) & 0xffff))
#endif//取DWORD的高位WORD
#ifndef HI_WORD
#define HI_WORD(l) ((unsigned short)((((unsigned __int64)(l)) >> 16) & 0xffff))
#endif//取WORD的低位BYTE
#ifndef LO_BYTE
#define LO_BYTE(w) ((unsigned char)(((unsigned __int64)(w)) & 0xff))
#endif//取WORD的高位BYTE
#ifndef HI_BYTE
#define HI_BYTE(w) ((unsigned char)((((unsigned __int64)(w)) >> 8) & 0xff))
#endif