听说是微软的面试题
2写出下列程序在X86上的运行结果。
struct mybitfields
{
unsigned short a : 4; /* 提问1:这里的:4是什么意思?我孤陋寡闻,从来没见过啊 */
unsigned short b : 5;
unsigned short c : 7;
}test /*这里应该有个分号是吧*/
void main(void)
{
int i;
test.a=2;
test.b=3;
test.c=0;
i=*((short *)&test); /* 提问2:这句是难点吧?我猜了一下,可是与我上机验证的结果不符*/
printf("%d\n",i);
}
我验证了一下,结果是50
下面是我的程序
//micro_1.cpp
#include <iostream>
struct mybitfields
{
unsigned short a : 4; /*4不是赋值*/
unsigned short b : 5;
unsigned short c : 7;
}test;
int main()
{
int i;
test.a=2;
test.b=3;
test.c=0;
i=*((short *)&test);
std::printf("%d\n",i);
return 0;
}