C语言宏定义(常用宏定义)

常用宏定义


数值相关的宏定义

  1. 闰年的判断 ,年份可以整除4并且不能整除100,或者可以整除400,则为闰年;
#define IS_LEAP_YEAR(y) (((((y) % 4) == 0) && (((y) % 100) != 0))  \
   					 || (((y) % 400) == 0))/*判断是否是闰年*/
  1. **MAX 与 MIN ** ;
#define MAX(x, y)   (((x) < (y)) ? (y) : (x))	/*两数取最大数*/
#define MIN(x, y)   (((x) < (y)) ? (x) : (y))	/*两数取最小数*/
  1. BCD码
#define BCD2HEX(x) (((x) >> 4) * 10 + ((x) & 0x0F))       /*BCD码转数值, 20H -> 20*/
#define HEX2BCD(x) (((x) % 10) + ((((x) / 10) % 10) << 4))  /*数值转BCD码, 20 -> 20H*/

字符相关的宏定义

  1. 字符范围判断
/*字符是否在某个区间范围内*/
#define in_range(c, lo, up)  ((uint8)c >= lo && (uint8)c <= up) 	

#define isprint(c)           in_range(c, 0x20, 0x7f)
   /*十进制内字符*/
#define isdigit(c)           in_range(c, '0', '9')
/*十六进制内字符*/
#define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
/*是否是小写*/
#define islower(c)           in_range(c, 'a', 'z')	
/*是否是空格*/
#define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
/*是否为ASCII码*/
#define isascii(c)          ((unsigned) (c) <= 0177) 

byte相关的宏定义

#define MSB(x) (((x) >> 8) & 0xff) /* x占2byte(如short)2byte的高地址的1byte */
#define LSB(x) ((x) & 0xff)	/* x占2byte(如short)2byte的低地址的1byte*/

#define MSW(x) (((x) >> 16) & 0xffff) /* x占4byte(如int)  4byte的高地址的2byte */
#define LSW(x) ((x) & 0xffff) 		  			
#define WORDSWAP(x) (MSW(x) | (LSW(x) << 16)) /* x占4byte(如int) 低2字节与高2字节内容交换 */	

#define LLSB(x)	((x) & 0xff)	/*x占4byte(如int) 取低地址1byte*/					
#define LNLSB(x) (((x) >> 8) & 0xff)
#define LNMSB(x) (((x) >> 16) & 0xff)
#define LMSB(x)	 (((x) >> 24) & 0xff)
/*x占4byte(如int) 4字节逆序*/
#define LONGSWAP(x) ((LLSB(x) << 24) \ 			
					|(LNLSB(x) << 16) \
					|(LNMSB(x) << 8) \
					|(LMSB(x)))

bit相关的宏定义

/* 判断某位是否为1 */
#define BIT_IS_1(x,y) (((x)>>(y))&0x01u)   

#define SETBITS(x,y,n) (x) = (n) ? ((x)|(1 << (y))) : ((x) &(~(1 << (y))));
/* 给某位置反 */
#define BIT_INVERSE(x,y)    ((x)=(x)^(1<<(y)))  						
/* 字节串中某BIT值*/
#define BIT_OF_BYTES(x, y) (BITS(x[(y)/8], (y)%8))
/* 字节串中设置某BIT为0 */						
#define SETBITSTO0_OF_BYTES(x, y) (x[(y)/8]) &= (~(1 << ((y)%8)))
/* 字节串中设置某BIT为1 */		
#define SETBITSTO1_OF_BYTES(x, y) (x[(y)/8]) |= (1 << ((y)%8))			

数组与结构体相关的宏定义

/* number of elements in an array */
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) 							
/* byte offset of member in structure*/
#define MOFFSET(structure, member)	((int) &(((structure *) 0) -> member)) 		
/* size of a member of a structure */
#define MEMBER_SIZE(structure, member)	(sizeof (((structure *) 0) -> member)) 

对齐的宏定义

/*向上对齐,~(align - 1)为对齐掩码,例如align=8,~(align - 1) = ~7,
(~7)二进制后三位为000,&(~(align - 1)) = &(~7),就是去掉余数,使其能被8整除*/
#define ALIGN_UP(x, align)  (((int) (x) + (align - 1)) & ~(align - 1)) 
/*向下对齐*/
#define ALIGN_DOWN(x, align)    ((int)(x) & ~(align - 1))
/*是否对齐*/
#define ALIGNED(x, align)   (((int)(x) & (align - 1)) == 0)

/*页面对齐相关的宏,一页为4096字节*/
#define PAGE_SIZE         4096
#define PAGE_MASK         (~(PAGE_SIZE-1))
#define PAGE_ALIGN(addr) -(((addr)+PAGE_SIZE-1) & PAGE_MASK)
发布了62 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/native_lee/article/details/83821657