note
stevieb
<p>Here's a hastily thrown together short program that does all the work. Removes trailing zeros, counts the number of bits remaining in the number, and informs you whether the number fits within 53 bits.</p>
<c>#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define MAXLEN 53
unsigned int bitCount(unsigned long num);
uint8_t doesItFit(unsigned long num);
void main () {
int num = 0b10100000;
unsigned long bigNum = 18446744073709551615; // 2^64-1
printf(
"Fit? Small: %d, Big: %d\n",
doesItFit(num),
doesItFit(bigNum)
);
}
unsigned int bitCount(unsigned long num) {
unsigned int bits = 0;
while (num) {
bits++;
num >>= 1;
}
return bits;
}
uint8_t doesItFit (unsigned long num) {
num = num >> __builtin_ctz(num);
unsigned int numBits = bitCount(num);
return numBits <= MAXLEN ? 1 : 0;
}
</c>
<p>Output:</p>
<c>
Fit? Small: 1, Big: 0
</c>
11113419
11113419