int isPowOf2(double number) {
union {
double floatRepresent;
unsigned long long intRepresent;
} bitset;
bitset.floatRepresent = number;
long long t1 = bitset.intRepresent & ((1ULL << 52)-1);
long long t2 = bitset.intRepresent - 1;
if((bitset.intRepresent & ((1ULL << 52)-1)) != 0)
return ((bitset.intRepresent & (bitset.intRepresent-1)) == 0); // denormalized number
unsigned long long power = bitset.intRepresent >> 52;
return power > 0 && power < 2047; // 1023 = Infinity; higher values = negative numbers
}
Подскажите пожалуйста, какую функцию здесь выполняет строка return ((bitset.intRepresent & (bitset.intRepresent-1)) == 0); // denormalized number - как она проверяет число на денормализованое значение?
#include <stdint.h>
#include <string.h>
int isPowOf2(double number)
{
uint64_t v;
uint64_t m;
memcpy(&v, &number, sizeof(v));
m = v & ((UINT64_C(1) << 52) - 1); // m holds the mantissa bits
switch ((v >> 52) & 0x7ff) { // check the exponent bits
case 0: // 0 or denormal
if (m == 0) // 0 is represented by exponent and mantissa both 0
return 0; // 0 is not a power of 2
else // denormal number has all its bits in the mantissa
return m & (m - 1) == 0; // a power of 2 has exactly one 1 bit in m
case 0x7ff: // infinity or NaN
return 0; // neither is a power of 2
default: // ordinary number always has leading 1 which is not recorded in the mantissa
return m == 0; // a power of 2 has exactly one 1 bit and we know that it's not in m
}
}