#include "stdio.h"
#include "stdlib.h"
struct {
unsigned char i:4;
unsigned char j:4;
}s;
void main()
{
for (s.i = 1; s.i <= 9; s.i++)
{
for (s.j = 1; s.j <= 9; s.j++)
{
if(s.i % 3 != s.j % 3)
printf("i的坐标数%d,j的坐标数%d\n",s.i,s.j);
}
}
}
位域
有些信息在存储时,并不需要占用一个完整的字节,
struct
{
其中位域列表的形式为:
例如:
struct
{
int
int
int
};
位域变量的说明与结构变量说明的方式相同。
struct
{
int
int
int
}data;
说明data为bs变量,共占两个字节。其中位域a占8位,位域b占2位,位域c占6位。对于位域的定义尚有以下几点说明:
1.
struct
{
unsigned
unsigned
unsigned
unsigned
}
在这个位域定义中,a占第一字节的4位,后4位填0表示不使用,b从第二字节开始,占用4位,c占用4位。
2.
3.
struct
{
int
int
int
int
};
从以上分析可以看出,位域在本质上就是一种结构类型,
二、位域的使用位域的使用和结构成员的使用相同,其一般形式为:
main(){
struct
{
unsigned
unsigned
unsigned
}
bit.a=1;
bit.b=7;
bit.c=15;
printf("%d,%d,%d\n",bit.a,bit.b,bit.c);
pbit=&bit;
pbit->a=0;
pbit->b&=3;
pbit->c|=1;
printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c);
}
上例程序中定义了位域结构bs,三个位域为a,b,c。说明了bs类型的变量bit和指向bs类型的指针变量pbit。这表示位域也是可以使用指针的。
程序的9、10、11三行分别给三个位域赋值。(