C语言中位域的使用及整体赋值
2010-01-16 21:08
246 查看
#include <stdio.h>
typedef struct _byte ByteType;
struct _byte /*定义一个字节中的每个位,当然int是十六位的*/
{
int b0:1,
b1:1,
b2:1,
b3:1,
b4:1,
b5:1,
b6:1,
b7:1;
};
void main(void)
{
ByteType byte;
int b;
byte.b0=0;
byte.b1=0;
byte.b2=0;
byte.b3=0;
byte.b4=0;
byte.b5=0;
byte.b6=0;
byte.b7=0;
(int)*((int*)&byte)=0x3;
b=0x3;
// byte=0x03;
b=(int)*((int*)&byte); //-858993460系统默认为这个数,转换为十进制数加上858993664
printf("%x/n",b);
b=3;
(int)*((int*)&byte)=4;
b=(int)*((int*)&byte);
printf("%x/n",b); //此处打印出4
if(byte.b0)
printf("hello");
printf("%x/n",byte);
printf("%x",b);
}
typedef struct _byte ByteType;
struct _byte /*定义一个字节中的每个位,当然int是十六位的*/
{
int b0:1,
b1:1,
b2:1,
b3:1,
b4:1,
b5:1,
b6:1,
b7:1;
};
void main(void)
{
ByteType byte;
int b;
byte.b0=0;
byte.b1=0;
byte.b2=0;
byte.b3=0;
byte.b4=0;
byte.b5=0;
byte.b6=0;
byte.b7=0;
(int)*((int*)&byte)=0x3;
b=0x3;
// byte=0x03;
b=(int)*((int*)&byte); //-858993460系统默认为这个数,转换为十进制数加上858993664
printf("%x/n",b);
b=3;
(int)*((int*)&byte)=4;
b=(int)*((int*)&byte);
printf("%x/n",b); //此处打印出4
if(byte.b0)
printf("hello");
printf("%x/n",byte);
printf("%x",b);
}