typedef struct
{
unsigned long int a:1;
unsigned long int b:1;
.
.
依次类推
.
unsigned long int u:1;
unsigned long int :11;
}A;
A data[21];
引用时data[3].e就是你所说的a[3][5] |
| [tr][/tr]
贴上代码,如果_id值是常数的话,由于编译器的优化,一般可以产生比较好的代码,当然如果是变量的话,也是支持的
#define BIT_SET(_buf,_id) \
{ ((char *)(buf))[(_id)/(sizeof(char)<<3)] |= 1<<(_id)%(sizeof(char)<<3); }
#define BIT_CLR(_buf,_id) \
{ ((char *)(buf))[(_id)/(sizeof(char)<<3)] &= ~(1<<(_id)%(sizeof(char)<<3)); }
#define BIT_GET(_buf,_id) \
(((char *)(buf))[(_id)/(sizeof(char)<<3)] >> ((_id)%(sizeof(char)<<3)) & 0x01)
#define BIT_ROW_MAX 21
#define BIT_SET2(_buf,_max,_x,_y) BIT_SET((_buf),((_x)*(_max)+(_y)))
#define BIT_CLR2(_buf,_max,_x,_y) BIT_CLR((_buf),((_x)*(_max)+(_y)))
#define BIT_GET2(_buf,_max,_x,_y) BIT_GET((_buf),((_x)*(_max)+(_y)))
int main(int argc, char* argv[])
{
char buf[256]={0};
int s=5;;
BIT_SET(buf,25);
s = BIT_GET(buf,25);
printf("25=%d\n",s);
BIT_CLR(buf,25);
s = BIT_GET(buf,25);
printf("25=%d\n",s);
BIT_SET2(buf,21,3,2);
BIT_CLR2(buf,21,2,3);
s = BIT_GET2(buf,21,3,2);
printf("2,3=%d\n",s);
BIT_CLR2(buf,21,2,3);
s = BIT_GET2(buf,21,2,3);
printf("2,3=%d\n",s);
printf("Hello World!\n");
return 0;
}
这两位大神提供了2维bit类型的数组,无奈我实在是看不懂 |
|