Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: reliably test integer size for portable bit-fiddling? (~0)

by tye (Sage)
on Dec 09, 2014 at 00:04 UTC ( [id://1109663]=note: print w/replies, xml ) Need Help??


in reply to reliably test integer size for portable bit-fiddling?

log(~0)/log(2)

- tye        

Replies are listed 'Best First'.
Re^2: reliably test integer size for portable bit-fiddling? (~0)
by davido (Cardinal) on Dec 09, 2014 at 00:50 UTC

    Beautiful use of the Maths. ;)


    Dave

      *cough*
      $ perl -E " say log(~0)/log(2) " 31.9999999996641 $ perl -E " say int 0.999+log(~0)/log(2) " 32
        $ perl -le "print log(~0+1)/log(2)" 32

        or, to be safe

        $ perl -e 'printf "%.0f\n", log(~0+1)/log(2)' 32

        - tye        

Re^2: reliably test integer size for portable bit-fiddling? (~0)
by wollmers (Scribe) on Dec 09, 2014 at 08:46 UTC
    log(~0)/log(2)

    Hmm, I tried a similar idea (get the index of the highest bit), but it failed. Now I remember:

    helmut@w3:~$ perl -e 'print log(-1)/log(2),"\n";' Can't take log of -1 at -e line 1.

    Thx, this is extremely fast and IMHO reliable, because it tests the capabilty directly.

    Helmut "wollmers" Wollmersdorfer

Re^2: reliably test integer size for portable bit-fiddling? (~0)
by wrog (Friar) on Dec 14, 2014 at 08:47 UTC
    I figure for base2 stuff length is better than log (no need to worry about rounding/transcendental crap), e.g.,
    4*length sprintf'%x',~0

      Avoid the multiply:

      say length sprintf "%b", ~0;; 64

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

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

    No recent polls found