首页 > 代码库 > bitmap
bitmap
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);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。