字节对齐
源自 RT-Thread 源码;
1.向上对齐
/**
* @ingroup BasicDef
*
* @def RT_ALIGN(size, align)
* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
* would return 16.
*/
#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
2.向下对齐
/**
* @ingroup BasicDef
*
* @def RT_ALIGN_DOWN(size, align)
* Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
* would return 12.
*/
#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
填充为偶数
#define PAD_EVEN(x) ( ((x)+1) & ~1 )
int g = PAD_EVEN(8);
int k = PAD_EVEN(9);
printf("g=%d k=%d\n",g,k);