<pre name="code" class="cpp">#include<stdio.h>
int main()
{
<span style="white-space:pre"> </span>int i;
<span style="white-space:pre"> </span>unsignd int x = (unsigned int)~0;
<span style="white-space:pre"> </span>for (i = 1; x >>= 8; i++);
<span style="white-space:pre"> </span>printf("%d\n", i);
}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
#include<stdio.h>
int main()
{
<span style="white-space:pre"> </span>int a = 0;
<span style="white-space:pre"> </span>printf("%d",(char *)(&a + 1) - (char *)(&a));
}

本文通过两个C语言示例程序介绍了如何使用位运算来计算整数的位数及内存中变量的字节对齐情况。第一个程序展示了通过右移运算符计算无符号整数的位数,第二个程序则演示了如何确定内存中整型变量的字节对齐方式。
947

被折叠的 条评论
为什么被折叠?



