当我运行以下代码时,我观察到奇怪的行为。我通过使用一个结构创建一个位域,我想在其中使用 52 位,所以我使用 long int。我的系统上 long int 的大小是 64 位,我在代码中检查它。不知何故,当我尝试设置一位时,它总是设置两位。其中一个是我想要设置的,第二个是第一个加 32 的索引。谁能告诉我,这是为什么呢?
#include <stdio.h>
typedef struct foo {
long int x:52;
long int:12;
};
int main(){
struct foo test;
int index=0;
printf("%ld\n",sizeof(test));
while(index<64){
if(test.x & (1<<index))
printf("%i\n",index);
index++;
}
test.x=1;
index=0;
while(index<64){
if(test.x & (1<<index))
printf("%i\n",index);
index++;
}
return 0;
}
Sry 忘了发布输出,所以我的问题基本上无法理解......它给我的输出如下:
8
0
32