3Fermer5
GodzilLe 18/04/2007 à 14:47
heu
Determining if an integer is a power of 2

unsigned int v; // we want to see if v is a power of 2
bool f; // the result goes here

f = (v & (v - 1)) == 0;

Note that 0 is incorrectly considered a power of 2 here. To remedy this, use:

f = !(v & (v - 1)) && v;


f = v && !(v&0x1); suffit largement que je sache