标签:style blog color os ar div amp log
struct bitmap { unsigned int len; unsigned char* buf; }; struct bitmap* creat_bitmap(unsigned int len) { struct bitmap *bm; if (len <=0) return(NULL); if ((bm = malloc(sizeof(struct bitmap))) == NULL) return(NULL); if ((bm->buf = malloc(len)) == NULL) { free(bm); return(NULL); } memset(bm->buf, 0, len); bm->len = len; return(bm); } void free_bitmap(struct bitmap* bm) { if (!bm) return; if (bm->buf) free(bm->buf); free(bm); } int set_bitmap_pos(struct bitmap* bm, unsigned int index, int value) { unsigned int x = index / 8; unsigned int y = index % 8; unsigned char mask = 0x80; if (index > bm->len) return(-1); if (y == 0) { if (x > 0) { --x; y = 7; } } if (value) bm->buf[x] |= mask>>y;//把buf的第index bit置1 else bm->buf[x] &= ~(mask>>y);//把buf的第index bit置0 return(1); } //检查bitmap的第 index bit 的值 int check_bitmap(struct bitmap* bm, unsigned int index) { unsigned int x = index / 8; unsigned int y = index % 8; unsigned char mask = 0x80; if (index > bm->len) return(-1); if (y == 0) { if (x > 0) { --x; y = 7; } } return(bm->buf[x] & (mask>>y)?1:0); }
标签:style blog color os ar div amp log
原文地址:http://www.cnblogs.com/emailck/p/3914408.html