如何会这样?
怎么会这样??
#include <stdio.h>
typedef struct
{
int a:1;
int b:2;
int c:1;
}test;
void main()
{
test t;
t.a = 1;
t.b = 2;
t.c = 3;
printf( "%d ",t.a);
printf( "%d ",t.b);
printf( "%d ",t.c);
getchar();
}
输出居然不是123!!!
怎么回事??
高手指教!!
------解决方案--------------------
http://topic.csdn.net/t/20060911/21/5014638.html
http://topic.csdn.net/t/20060807/22/4933577.html
参考这些帖子先
------解决方案--------------------
需要将int改成unsigned int
否则会有补码等问题~
------解决方案--------------------
#include <stdio.h>
typedef struct
{
unsigned int a:1;
unsigned int b:2;
unsigned int c:2;
}test;
不是123 因为位域和普通的类型一样只是数据的位少了
你用int 那么需要占用一个符号位 必须定义为unsigned或则增加位的数目
#include <stdio.h>
typedef struct
{
int a:1;
int b:2;
int c:1;
}test;
void main()
{
test t;
t.a = 1;
t.b = 2;
t.c = 3;
printf( "%d ",t.a);
printf( "%d ",t.b);
printf( "%d ",t.c);
getchar();
}
输出居然不是123!!!
怎么回事??
高手指教!!
------解决方案--------------------
http://topic.csdn.net/t/20060911/21/5014638.html
http://topic.csdn.net/t/20060807/22/4933577.html
参考这些帖子先
------解决方案--------------------
需要将int改成unsigned int
否则会有补码等问题~
------解决方案--------------------
#include <stdio.h>
typedef struct
{
unsigned int a:1;
unsigned int b:2;
unsigned int c:2;
}test;
不是123 因为位域和普通的类型一样只是数据的位少了
你用int 那么需要占用一个符号位 必须定义为unsigned或则增加位的数目