c实现bitmap

直接上代码.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 8972654 //随便设定的数
#define SHIFT 5
int bitmap[1 + MAX/32];

int setbit(unsigned int x);
int clearbit(unsigned int x);
void print_map();
void print_bit(int offset, int i);

int 
main(int argc, char *argv[]){
	if(argc < 2){
		printf("argc must greater than 1\n");
		return 1;
	}
	
	unsigned x = (unsigned)atoi(argv[1]);
	memset(bitmap, 0, sizeof(bitmap));

	if(setbit(x)){
		print_map();

		if(clearbit(x))
			print_map();
	}
	
	return 0;
}

int
setbit(unsigned int x){
	if(x > MAX || x == 0){
		printf("input should greater than 0\n");
		return 0;
	}else{
		bitmap[(x - 1) >> SHIFT] |= (1 << ((x - 1) & 31));
		return 1;
	}
}

int
clearbit(unsigned int x){
	if(x > MAX || x == 0){
		printf("input should greater than 0\n");
		return 0;
	}else{
		bitmap[(x - 1) >> SHIFT] &= ~(1 << ((x - 1) & 31));
		return 1;
	}
}

void
print_map(){
	int i;
	for(i = 0; i < (1 + MAX/32); i ++){
		print_bit(bitmap[i], i);	
	}
}

void
print_bit(int offset, int i){
	int j;
	for(j = 0; j < 32; j ++){
		if(offset & (1 << j)){
			printf("%d\n", i * 32 + j + 1);				
		}
	}
}

转载于:https://www.cnblogs.com/traxex/archive/2013/05/14/5850890.html

### C语言中Bitmap数据结构的实现方法与代码示例 在C语言中,Bitmap位图)是一种高效的数据结构,用于存储和操作大量的布尔型数据。其核心思想是使用每一位来表示一种状态,通常用于需要快速查找、插入和删除的应用场景[^2]。 以下是一个简单的Bitmap实现示例,展示了如何定义、初始化以及操作Bitmap数据结构: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define BITMAP_SIZE 1024 // 定义Bitmap的大小为1024位 // 定义Bitmap结构体 typedef struct { unsigned char *bits; // 使用unsigned char数组存储位 int size; // Bitmap的大小(以位为单位) } Bitmap; // 初始化Bitmap void bitmap_init(Bitmap *bitmap, int size) { bitmap->size = size; bitmap->bits = (unsigned char *)malloc((size / 8) + 1); // 分配足够的字节空间 memset(bitmap->bits, 0, (size / 8) + 1); // 将所有位初始化为0 } // 设置某一位为1 void bitmap_set(Bitmap *bitmap, int pos) { if (pos >= 0 && pos < bitmap->size) { bitmap->bits[pos / 8] |= (1 << (pos % 8)); // 设置指定位置的位为1 } } // 清除某一位为0 void bitmap_clear(Bitmap *bitmap, int pos) { if (pos >= 0 && pos < bitmap->size) { bitmap->bits[pos / 8] &= ~(1 << (pos % 8)); // 清除指定位置的位为0 } } // 检查某一位是否为1 int bitmap_test(Bitmap *bitmap, int pos) { if (pos >= 0 && pos < bitmap->size) { return (bitmap->bits[pos / 8] & (1 << (pos % 8))) != 0; // 检查指定位置的位是否为1 } return 0; } // 销毁Bitmap void bitmap_destroy(Bitmap *bitmap) { free(bitmap->bits); // 释放分配的内存 } // 测试函数 int main() { Bitmap bitmap; bitmap_init(&bitmap, BITMAP_SIZE); // 设置某些位 bitmap_set(&bitmap, 5); bitmap_set(&bitmap, 10); bitmap_set(&bitmap, 15); // 检查某些位 printf("Bit 5 is %s\n", bitmap_test(&bitmap, 5) ? "set" : "cleared"); printf("Bit 10 is %s\n", bitmap_test(&bitmap, 10) ? "set" : "cleared"); printf("Bit 15 is %s\n", bitmap_test(&bitmap, 15) ? "set" : "cleared"); // 清除某些位 bitmap_clear(&bitmap, 10); // 再次检查 printf("After clearing, Bit 10 is %s\n", bitmap_test(&bitmap, 10) ? "set" : "cleared"); // 销毁Bitmap bitmap_destroy(&bitmap); return 0; } ``` 上述代码实现Bitmap的基本功能,包括初始化、设置位、清除位、测试位以及销毁Bitmap的操作。通过这种方式,可以有效地管理和操作大规模的布尔型数据集合[^2]。 ### 注意事项 - Bitmap的大小应根据实际需求进行调整。 - 在操作Bitmap时,需要注意边界条件,避免越界访问。 - 使用完毕后,务必调用`bitmap_destroy`函数释放分配的内存,防止内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值