Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: reliable get position of leftmost bit in large integers

by hdb (Monsignor)
on Mar 12, 2015 at 10:42 UTC ( [id://1119768]=note: print w/replies, xml ) Need Help??


in reply to reliable get position of leftmost bit in large integers

Should your code not be:

perl -e 'for my $i (1..63) {if(int(log(2**($i-1))/log(2)) != $i-1) {pr +int "$i\n"}}'

which gives no output for me.

UPDATE: Apologies, I think I get it now, the leading one of 2^i - 1 should be in the same place as the leading one for 2^(i-1) which is what you are testing.

My next thought was to use length(sprintf("%b",$x)) but that does not work either:

for my $i (1..63) { if(length(sprintf("%b",2**$i-1)) != length(sprintf("%b",2**($i-1)) +)) { print"$i\n"; printf("%b\n",2**($i-1)); printf("%b\n",(2**$i)-1); } }

Replies are listed 'Best First'.
Re^2: reliable get position of leftmost bit in large integers
by wollmers (Scribe) on Mar 12, 2015 at 11:09 UTC

    No, because the problem triggers on vectors filled with on-bits. But the exponentiation ** also seems to have a problem (thanks your remark):

    $ perl -e 'print sprintf("%b",2**53-1),"\n"' 11111111111111111111111111111111111111111111111111111 $ perl -e 'print sprintf("%b",2**54-1),"\n"' 1000000000000000000000000000000000000000000000000000000 $ perl -e 'print sprintf("%b",2**(53-1)),"\n"' 10000000000000000000000000000000000000000000000000000

      The problem is that ** returns a double (floating point), which explains the lack of precision. You should use 1<<54 like BrowserUk did in his answer.

        You should use 1<<54 like BrowserUk did in his answer.

        Even old dogs can learn :)


        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". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

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

    No recent polls found