yuxiangxyz
02-21 10:22
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值是常數(shù)的話,由于編譯器的優(yōu)化,一般可以產(chǎn)生比較好的代碼,當(dāng)然如果是變量的話,也是支持的
#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類型的數(shù)組,無奈我實在是看不懂