码迷,mamicode.com
首页 > 其他好文 > 详细

bitmap

时间:2014-08-15 12:46:48      阅读:213      评论:0      收藏:0      [点我收藏+]

标签: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);
}

 

bitmap,布布扣,bubuko.com

bitmap

标签:style   blog   color   os   ar   div   amp   log   

原文地址:http://www.cnblogs.com/emailck/p/3914408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!