技术的乐趣在于分享,欢迎多多交流,多多沟通。
全部博文(877)
分类: LINUX
2014-07-29 14:27:55
//2,4,8字节对齐方法测试
#include
int main(void)
{
int var = 0, i = 0, foo = 0,value = 0;
for(; i < 5; i++)
{
// var = i +( 1U & ~1U ); //这样写不会产生对齐效果 原因是()内的值为零
// foo = i +( 7U & ~7U );
// value = i + ( 3U & ~3U );
var = i + 1U & ~1U ; //Round number of bytes to nearest 2-byte boundary 2字节对齐
value = i + 3U & ~3U ; //Round number of bytes to nearest 4-byte boundary 4字节对齐
foo = i + 7U & ~7U ; //Round number of bytes to nearest 8-byte boundary 8字节对齐
printf("The num var is '%d'\n",var);
printf("The num foo is '%d'\n",value);
printf("The num foo is '%d'\n",foo);
}
printf("Hello World!\n");
return 0;
}