C 中的位图,二进制运算
我即将创建一个控制 512 位的位图,所有 512 位都应该为 0,以表明程序启动时该位是空闲的。函数占领数据块(int number)应该找到空闲位,将该位的位置放入数组 int data_blocks[] 中,并将占用位设置为 1。
下面是一些执行某些工作的代码:除了 char bit[512/8 ] 是在函数内部声明的,因此当我调用艺占数据块时,将声明数组,这会产生相同的输出,并且在我的程序中出现很大的失败,当我尝试将 char 位声明为全局变量时,程序返回没有足够的内存。
我需要帮助来获取实现此目的的代码,并将该位设置为 占领。请给我一个编码手,我对解决方案有所了解,但无法用 C 语言表达。
#include <stdio.h>
#include <string.h>
void occupyDataBlocks(int number)
{
int ab = number;
char bit[512/8];
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
}
}
int main(void)
{
occupyDataBlocks(3);
occupyDataBlocks(3);
return 1;
}
#include <stdio.h>
#include <string.h>
int occupyDataBlocks(char bit, int number)
{
int ab = number;
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
return 0;
}
return 1;
}
int main(void)
{
unsigned char bit[512/8];
/*
* I need 3 data blocks that is ready for me to use. Put the position to the free data block in array data_blocks[],
* where data_blocks[0] can be 100 (the first free data block), data_block[1] = 400 (second free datablock) etc.
*
*/
int data_blocks[3];
memcpy(data_blocks, occupyDataBlocks(bit, 3), sizeof(data_blocks));/* A try to copy the result of occypyDataBlocks to data_blocks*/
return 1;
}
I am about to create a bitmap that holds control of 512 bits, all 512 bits should be 0 to indicate that the bit is free when the program starts. Function occupyDataBlocks(int number) should find free bits put the position of the bit in the array int data_blocks[] and set the occupied bit to 1.
Under is some code that does some of the work: Except that char bit[512/8] is declared inside the function so the array will be declared when I call occupyDataBlocks something that makes the same output and a big fail in my prog, the programs return Not enough memory when I try to declare char bit as an global variable.
I need help to get the code to achieve this, and to set the bit to
occupied. Please give me a coding hand, I have a understanding of the solutions but cant express it in C.
#include <stdio.h>
#include <string.h>
void occupyDataBlocks(int number)
{
int ab = number;
char bit[512/8];
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
}
}
int main(void)
{
occupyDataBlocks(3);
occupyDataBlocks(3);
return 1;
}
#include <stdio.h>
#include <string.h>
int occupyDataBlocks(char bit, int number)
{
int ab = number;
int bitNum = 0;
int count;
int data_blocks[ab];
int b = 0;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[b++] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
return 0;
}
return 1;
}
int main(void)
{
unsigned char bit[512/8];
/*
* I need 3 data blocks that is ready for me to use. Put the position to the free data block in array data_blocks[],
* where data_blocks[0] can be 100 (the first free data block), data_block[1] = 400 (second free datablock) etc.
*
*/
int data_blocks[3];
memcpy(data_blocks, occupyDataBlocks(bit, 3), sizeof(data_blocks));/* A try to copy the result of occypyDataBlocks to data_blocks*/
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定你想做什么,阅读你的代码。
要么太混乱,要么太错误。
1.) 如果您想“打破”多个循环,您可能需要为循环编写一个辅助函数,然后“返回”。
2.) 我不确定你是否知道这一点:b++ 的意思是:返回 b 的值,然后递增变量。
3.) 运营商和和 |和 ^ 是按位的,&&和 ||和 !是布尔运算符,它们隐式地将 0/NULL 转换为 false/0,将其他所有内容转换为 true/1。所以,(x & 0) 总是等于 0,而 !(x & 0) 等于 1。
4.) 我对此不太确定,我的 C 语言知识并不完整:什么是值这:
字符正式既不是签名字符也不是未签名字符;它们是不同的类型,可能类似于有符号或无符号字符,具体取决于编译器。
5.) 此代码等于:
使用 for 代替 while 可能会让事情变得清晰,但是应该明确使用 for 的这三个参数来使事情变得清晰。对于编译器来说这不是那么相关,但是对于读者来说。
6.) sizeof(char) 始终为 1。您可能想要写的是这样的:
(sizeof(myarray)/sizeof(myarray[0]))
I'm not sure, what you want to do, reading your code.
Either it is too confusing or too bugy.
1.) If you want to "break" out of more than one loop, you might want to write a helper function for the loops, and "return" instead.
2.) I'm not sure wether you know this: b++ means: Return the value of b and then increment the variable afterwards.
3.) The operators & and | and ^ are bitwise, && and || and ! are boolean operators, which implicitely cast the 0/NULL to false/0 and everything else to true/1. So, (x & 0) always equals to 0, and !(x & 0) equals to 1.
4.) I'm not that sure about this, my C-knowledge is not complete: What is the value of this:
chars are officially neither signed chars nor unsigned chars; they are different types, which might be similar to signed or unsigned chars, depending on the compiler.
5.) This code equals:
Using a for instead of while might make things clear, but those three parameters of the for should be explicitely used to make things clear. For the compiler this is not that relevant, but for the reader.
6.) sizeof(char) is always 1. What you might want to write is sth like this:
(sizeof(myarray)/sizeof(myarray[0]))
正如您所提到的,数组
bit
需要在函数ocpyDataBlocks()
之外维护状态。对于这个程序,您可以在main()
中定义它们并将其传递给函数,因为我仍然不清楚为什么使用
int data_blocks[]
数组。如果您用更精确的要求和两个示例更新您的问题,那么我也许能够提供更好的建议。As you mentioned, the array
bit
need to maintain state outside of the functionoccupyDataBlocks()
. For this program, you can define them inmain()
and pass it to the function asI am still unclear why the
int data_blocks[]
array. If you update your question with more precise requirements and two examples, then I may be able to offer better suggestions.一些可能有帮助的要点:
malloc()
。(x & 0)
将始终等于0
。if (count == number)
没有任何意义。Some points that may help:
int data_blocks[ab];
. You may need to usemalloc()
.(x & 0)
will always equal0
.if (count == number)
doesn't make any sense.