php - Find the highest set bit -


i have 5 different values saved bits 10010. value int database (cannot change that) 24 means 11000 know can biggest bit here using

if ((decbin($d) & 16) == 16) 

but if first 0 have check next bit, , if 0 have ...

so after have block of ifs , if there more bits block bigger. there simple way "id" (or value, not matter) of highest bit 1?

yes. compute base 2 logarithm of number , floor it:

$highbit = floor(log($d, 2)); 

if $highbit, instance, 5, means 5th bit highest bit set 1.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -