常用C代码碎片:
1、函数参数接口
typedef int(*pfunc)(int x, int y) //pfunc是带两个参数,返回值为int
pfunc handler
2、宏函数、宏字符串连接
#define joiner(param1, param2) param1##param2
#define tostring(param) #param
3、位赋值操作
typedef struct
{
u8 lv1:1;//故障等级1 u8类型的低位
u8 lv2:1;//故障等级2
u8 lv3:1;//故障等级3
u8 max_lv:2;//最高故障等级(单个故障)
u8 res:3;//保留 u8类型的高位
}SINGLE_FLV_FLAG_BIT;//单个故障等级标志位位域
4、统计二进制数中的个数
int func(int x) {
int countx = 0;
while(x) {
countx++;
x = x &(x-1);
}
return countx;
}
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/275638.html