LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

get number of bits in integer without converting to string first

I know how to get the property node; now if someone could just tell me how to make a global....Smiley Very Happy

Message 11 of 13
(625 Views)

@for(imstuck) wrote:

Is there a way to "get number of bits in integer without converting to string first" without converting to a string first to use the string length function? 


Number of bits that represent a given integer?

 

floor( log2( n ) ) + 1;

 

Where n is the number you're determining the bit length of.  floor() is the floor() function, which literally drops the decimal (with no rounding).  log2 calculates the binary logarithm (log with base 2) and 1, well, is the loneliest number.  Smiley Very Happy

 

The secret here is that you don't care about any binary bit except the highest order bit with a 1, and its "position" tells you exactly how many bits (minus 1) you need to represent that number.  Binary positions are represented as 2^n, and the reverse is log2 - so the whole digit in the log2 calculation tells you the position of the highest-order bit, and everything else is just fluff.

 

EDIT:  LOL, I almost forgot to add 1.  When we say how many bits we're using, we're 1-based, not zero-based, so bit position only gives us "almost" the full answer...  🙂

 

-Danny

0 Kudos
Message 12 of 13
(599 Views)

I am pretty sure if he tries that formula out on -1 he is going to encounter an out of memory error somewhere.  Smiley Surprised

 

I think he wants the bits in his chosen representation, not a given value.

0 Kudos
Message 13 of 13
(579 Views)