Find the first zero in a bitarray

前端 未结 3 1055
闹比i
闹比i 2020-12-18 12:02

I have a bitmap

uint64_t bitmap[10000] 

to keep track of the resources allocated in the system. Now the question is how do efficiently I

3条回答
  •  情深已故
    2020-12-18 12:51

    I'm not sure how you'd get much faster than this, but I'm open to being proven wrong:

    uint64_t bitmap[10000];
    unsigned int i;
    for (i = 0; i < (sizeof(bitmap) / sizeof(*bitmap)) && 0xFFFFFFFFFFFFFFFF == bitmap[i]; ++i);
    const int bitInWord = ffsll(bitmap[i]);
    const unsigned int firstZeroBit = bitInWord ? i * sizeof(*bitmap) * CHAR_BIT + bitInWord : 0;
    

提交回复
热议问题