Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Detecting whether UV fits into an NV

by stevieb (Canon)
on Feb 26, 2020 at 02:33 UTC ( [id://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; }

Output:

Fit? Small: 1, Big: 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found