C 中的位图,二进制运算

发布于 2024-09-27 12:06:59 字数 2939 浏览 2 评论 0原文

我即将创建一个控制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝在阴天搁浅。 2024-10-04 12:06:59

我不确定你想做什么,阅读你的代码。
要么太混乱,要么太错误。

1.) 如果您想“打破”多个循环,您可能需要为循环编写一个辅助函数,然后“返回”。

2.) 我不确定你是否知道这一点:b++ 的意思是:返回 b 的值,然后递增变量。

b++ equals:
{int x=b; b=b+1; return x;}

++b equals:
{b=b+1; return b;}

3.) 运营商和和 |和 ^ 是按位的,&&和 ||和 !是布尔运算符,它们隐式地将 0/NULL 转换为 false/0,将其他所有内容转换为 true/1。所以,(x & 0) 总是等于 0,而 !(x & 0) 等于 1。

4.) 我对此不太确定,我的 C 语言知识并不完整:什么是值这:

((signed char)0xff) >> 1  == 0xff
or
((signed char)0xff) >> 1  == 0x7f

字符正式既不是签名字符也不是未签名字符;它们是不同的类型,可能类似于有符号或无符号字符,具体取决于编译器。

5.) 此代码等于:

for(a,aa;b;c,cc)d,dd;

{a;aa; while(b){d;dd;c;cc;} }

使用 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.

b++ equals:
{int x=b; b=b+1; return x;}

++b equals:
{b=b+1; return b;}

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:

((signed char)0xff) >> 1  == 0xff
or
((signed char)0xff) >> 1  == 0x7f

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:

for(a,aa;b;c,cc)d,dd;

{a;aa; while(b){d;dd;c;cc;} }

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]))

苯莒 2024-10-04 12:06:59

正如您所提到的,数组 bit 需要在函数 ocpyDataBlocks() 之外维护状态。对于这个程序,您可以在 main() 中定义它们并将其传递给函数,因为

int main() {

    unsigned char bit[512/8];      // Note: unsigned

    occupyDataBlocks( bit, 3 );
    occupyDataBlocks( bit, 3 );
}

我仍然不清楚为什么使用 int data_blocks[] 数组。如果您用更精确的要求和两个示例更新您的问题,那么我也许能够提供更好的建议。

As you mentioned, the array bit need to maintain state outside of the function occupyDataBlocks(). For this program, you can define them in main() and pass it to the function as

int main() {

    unsigned char bit[512/8];      // Note: unsigned

    occupyDataBlocks( bit, 3 );
    occupyDataBlocks( bit, 3 );
}

I 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.

伪装你 2024-10-04 12:06:59

一些可能有帮助的要点:

  • 声明“可变大小数组”(例如 int data_blocks[ab]; )是无效的 C(C99 除外)。您可能需要使用malloc()
  • (x & 0)始终等于0
  • if (count == number) 没有任何意义。
  • 您需要弄清楚如果没有足够的可用位会发生什么。

Some points that may help:

  • It is not valid C (except in C99) to declare "variable-sized arrays" such as int data_blocks[ab];. You may need to use malloc().
  • (x & 0) will always equal 0.
  • if (count == number) doesn't make any sense.
  • You need to work out what happens if there aren't enough free bits.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文