integer - Find out how often can x be divided by 2 without loop in C -


i'm looking way find how can divide constant x 2 (and not remainder) without using loops, recursion or logarithm. since same problem finding index of least significant non-zero bit, hoping there way use bitwise operations this. unfortunately wasn't able come it. ideas?

background: have loop doubles counter on every iteration until no longer divides constant x. want loop unrolled, nvidia cuda compiler isn't smart enough figure out number of iterations, want rewrite loop in such way number of iterations becomes more obvious compiler:

for(i=1; const & == 0; *= 2)      bla(i); 

should become like

#define iterations missing_expr_with_const for(i=0; < iterations; i++)      fasel(i); 

this can directly solved using this code 32 bit numbers (i take no credit).

unsigned int v;  // find number of trailing zeros in 32-bit v  int r;           // result goes here static const int multiplydebruijnbitposition[32] =  {   0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,    31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 }; r = multiplydebruijnbitposition[((uint32_t)((v & -v) * 0x077cb531u)) >> 27]; 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -