Home > Software engineering >  Check if N(large) bits are set
Check if N(large) bits are set

Time:04-06

I want to check if any of the 1024 bits (128 bytes) are set. If all are clear, then I want to do something. Is it possible to do this quickly, i.e one instruction or do I have to loop through my bitmap?

CodePudding user response:

Check whether any data is non zero. Let buffer be the data you want to check,

int n=128;
unsigned char *buffer = &data;
for(int i=0 ;i<n ; i  ) {
           if((*data)) {  //If data is non zero any of the bit in the data is 
                          // set, so quit from iterating.
                break;
           } 
           data  ;
}

CodePudding user response:

As i have understood the questions, you want to check if any of the bit is set out of 1024 bits you have.

Assuming you are on 64 bit machine. store the bits as array of unit64_t type which is 8 bytes.

So you have array of this kind.

uint64_t bits[12] = {0}; // 1024 bits

and to check your bit condition

for(int i = 0, j=0; i < 12; i  ){
    if(bits[i]) {
        return FAIL_CONDITION;
    }
}
return SUCCESS_CONDITION;
  • Related