早上遇到一个知识点,一个函数调用时入参传入值是unsigned long类型,这个值是一大堆16进制数据做或运算(即相加)而来,然后就写了一个或运算求值的方法自己使用
代码如下:
//定义16进制数据的宏
#define WFS_PIN_FK_0 (0x00000001)
#define WFS_PIN_FK_1 (0x00000002)
#define WFS_PIN_FK_2 (0x00000004)
#define WFS_PIN_FK_3 (0x00000008)
#define WFS_PIN_FK_4 (0x00000010)
#define WFS_PIN_FK_5 (0x00000020)
#define WFS_PIN_FK_6 (0x00000040)
#define WFS_PIN_FK_7 (0x00000080)
#define WFS_PIN_FK_8 (0x00000100)
#define WFS_PIN_FK_9 (0x00000200)
#define WFS_PIN_FK_ENTER (0x00000400)
#define WFS_PIN_FK_CANCEL (0x00000800)
#define WFS_PIN_FK_CLEAR (0x00001000)
#include <iostream>
using namespace std;
int main()
{
unsigned long ulKeysValue =0; //存放最后算出来的或运算值(即16进制相加后的值)
//16进制数据的数组
unsigned long ulKeysValueArray[] = {WFS_PIN_FK_0,WFS_PIN_FK_1,WFS_PIN_FK_2,WFS_PIN_FK_3,WFS_PIN_FK_4,WFS_PIN_FK_5,WFS_PIN_FK_6,WFS_PIN_FK_7,WFS_PIN_FK_8,WFS_PIN_FK_9,WFS_PIN_FK_ENTER,WFS_PIN_FK_CANCEL,WFS_PIN_FK_CLEAR};
int nlen = sizeof(ulKeysValueArray)/sizeof(ulKeysValueArray[0]);
for(int i=0;i<nlen;i++)
{
ulKeysValue= ulKeysValue|ulKeysValueArray[i];
}
printf("nlen=%d
",nlen);
printf("value = %d
",ulKeysValue);
return 0;
}
测试结果如下: