C/C++ Bit Array or Bit Vector

前端 未结 5 1314
北荒
北荒 2020-12-05 12:29

I am learning C/C++ programming & have encountered the usage of \'Bit arrays\' or \'Bit Vectors\'. Am not able to understand their purpose? here are my doubts -

5条回答
  •  心在旅途
    2020-12-05 12:56

    Bit Arrays of Bit Vectors are used as a mapping from position to some bit value. Yes it's basically the same thing as an array of Bool, but typical Bool implementation is one to four bytes long and it uses too much space.

    We can store the same amount of data much more efficiently by using arrays of words and binary masking operations and shifts to store and retrieve them (less overall memory used, less accesses to memory, less cache miss, less memory page swap). The code to access individual bits is still quite straightforward.

    There is also some bit field support builtin in C language (you write things like int i:1; to say "only consume one bit") , but it is not available for arrays and you have less control of the overall result (details of implementation depends on compiler and alignment issues).

    Below is a possible way to answer to your "search missing numbers" question. I fixed int size to 32 bits to keep things simple, but it could be written using sizeof(int) to make it portable. And (depending on the compiler and target processor) the code could only be made faster using >> 5 instead of / 32 and & 31 instead of % 32, but that is just to give the idea.

    #include 
    #include 
    #include 
    
    int main(){
        /* put all numbers from 1 to 1000000 in a file, except 765 and 777777 */
        {
            printf("writing test file\n");
            int x = 0;
            FILE * f = fopen("testfile.txt", "w");
            for (x=0; x < 1000000; ++x){
                if (x == 765 || x == 777760 || x == 777791){
                    continue;
                }
                fprintf(f, "%d\n", x);
            }
            fprintf(f, "%d\n", 57768); /* this one is a duplicate */
            fclose(f);
        }
    
        uint32_t bitarray[1000000 / 32];
    
        /* read file containing integers in the range [1,1000000] */
        /* any non number is considered as separator */
        /* the goal is to find missing numbers */
        printf("Reading test file\n");
        {
            unsigned int x = 0;
            FILE * f = fopen("testfile.txt", "r");
            while (1 == fscanf(f, " %u",&x)){
                bitarray[x / 32] |= 1 << (x % 32);
            }
            fclose(f);
        }
        /* find missing number in bitarray */
        {
            int x = 0;
            for (x=0; x < (1000000 / 32) ; ++x){
                int n = bitarray[x];
                if (n != (uint32_t)-1){
                    printf("Missing number(s) between %d and %d [%x]\n",
                        x * 32, (x+1) * 32, bitarray[x]);
                    int b;
                    for (b = 0 ; b < 32 ; ++b){
                        if (0 == (n & (1 << b))){
                            printf("missing number is %d\n", x*32+b);
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题