Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Detecting whether UV fits into an NV

by stevieb (Canon)
on Feb 26, 2020 at 02:33 UTC ( #11113421=note: print w/replies, xml ) Need Help??

in reply to Detecting whether UV fits into an NV

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.

#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; }


Fit? Small: 1, Big: 0

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113421]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-04-07 18:08 GMT
Find Nodes?
    Voting Booth?
    The most amusing oxymoron is:

    Results (43 votes). Check out past polls.