Вот небольшой анализ (в «читаемой форме»):
usnigned int nx = ~x; // I suppose it's unsigned
int a = nx & (nx >> 1);
// a will be 0 if there are no 2 consecutive "1" bits.
// or it will contain "1" in position N1 if nx had "1" in positions N1 and N1 + 1
if (a == 0) return 0; // we don't have set bits for the following algorithm
int b = a ^ (a & (a - 1));
// a - 1 : will reset the least 1 bit and will set all zero bits (say, NZ) that were on smaller positions
// a & (a - 1) : will leave zeroes in all (NZ + 1) LSB bits (because they're only bits that has changed
// a ^ (a & (a - 1)) : will cancel the high part, leaving only the smallest bit that was set in a
// so, for a = 0b0100100 we'll obtain a power of two: b = 0000100
return b | (b << 1);
// knowing that b is a power of 2, the result is b + b*2 => b*3
Похоже, что алгоритм ищет первые 2 (начиная с LSB) последовательных 0
бита в переменной x
.
Если их нет, то результат равен 0.
Если они найдены, скажем, в позиции PZ
, то результат будет содержать два установленных бита: PZ
и PZ+1
.