c++ - Bits aren't being reset? -


i using bit scan forward detect set bits within unit64_t, use each set bit index within program, clear set bit , proceed find next set bit. however, when initial uint64_t value is:

0000000000001000000000000000000000000000000000000000000000000000

the below code isn't resetting 52nd bit, therefore gets stuck in while loop:

uint64_t bits64 = data;  //detects index being 52 int32_t index = __builtin_ffsll(bits64);  while(0 != index){      //my logic      //set bit isn't being cleared here     clearnthbitof64(bits64, index);      //still picks-up bit 52 being set     index = __builtin_ffsll(bits64); }  void clearnthbitof64(uint64_t& input, const uint32_t n) {     input &= ~(1 << n); } 

from the docs:

— built-in function: int __builtin_ffs (int x)
returns one plus index of least significant 1-bit of x, or if x zero, returns zero.

you're off 1 on clear function, should be:

clearnthbitof64(bits64, index-1); 

also clear function overflowing. need ensure you're left shifting of sufficient size:

void clearnthbitof64(uint64_t& input, const uint32_t n) {     input &= ~(1ull << n);     //          ^^^ } 

Comments