Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Bit fiddling madness

by sn1987a (Monk)
on Feb 11, 2014 at 14:58 UTC ( #1074428=note: print w/ replies, xml ) Need Help??


in reply to Bit fiddling madness

I notice that Net/CIDR/Lookup.pm has "use integer"

According to Shift Operators:

Note that both << and >> in Perl are implemented directly using << and >> in C. If use integer (see Integer Arithmetic) is in force then signed C integers are used."
This means that right shifts will be sign extended. Anything with the msb set will be sign extended and remain negative.

$ perl -E 'use integer; say 0x80000000 >> 31' -1

RichardK's suggestion above would avoid this issue.


Comment on Re: Bit fiddling madness
Download Code
Re^2: Bit fiddling madness
by Athanasius (Monsignor) on Feb 11, 2014 at 15:51 UTC

    ++sn1987a (when the Vote Fairy next visits) for an ingenious observation! Unfortunately, I don’t think it answers the OP’s question, as masking with bitwise-AND does remove the sign bit, even with use integer in effect:

    #! perl use strict; use warnings; print "\nno integer\n"; my $bit = -1 >> 31; printf "unmasked: 0x%x = %d\n", $bit, $bit; $bit = (-1 & 0x80000000) >> 31; printf "masked: 0x%x = %d\n", $bit, $bit; use integer; print "\nuse integer\n"; $bit = -1 >> 31; printf "unmasked: 0x%x = %d\n", $bit, $bit; $bit = (-1 & 0x80000000) >> 31; printf "masked: 0x%x = %d\n", $bit, $bit;

    Output:

    1:29 >perl 868_SoPW.pl no integer unmasked: 0x1ffffffff = 8589934591 masked: 0x1 = 1 use integer unmasked: 0xffffffffffffffff = -1 masked: 0x1 = 1 1:29 >perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x +86-multi-thread-64int

    :-(

    Update: As BrowserUk observes below, masking removes the sign bit (in this case) only in perls built with 64-bit ints. For perls built with 32-bit ints, sn1987a’s observation does answer the OP’s question. (Moral for self: test, test, test!)

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      built for MSWin32-x86-multi-thread-64int

      The problem only(*) occurs with 32-bit perls:

      C:\test>type CIDRtest.pl #! perl -slw use strict; use integer; for my $n ( 0 .. 31 ) { my $addr = (1 << $n) + 1; printf "\r%u", $addr; my $nbits = 32; while( 1 ) { my $bit = ( $addr & 0x80000000 ) >> 31; die "$addr -> $bit" if $bit != 0 and $bit != 1; $addr <<= 1; last unless --$nbits; } } C:\test>CIDRtest.pl 2147483649 C:\test>\perl32\bin\perl.exe CIDRtest.pl -2147483648 -> -1 at CIDRtest.pl line 11. 2

      (*Or 64-bit perls, but with much higher numbers.)


      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.
        built for MSWin32-x86-multi-thread-64int
        The problem only(*) occurs with 32-bit perls:

        Aaargh ... and hadn't even noticed that my laptop's system perl has 64 bit ints even though it's i686 so I'd used it for testingwhat I thought was a 32 bit system m(

        Many thanks everybody, that was the whack I needed!

      Thanks for the update Athanasius. I was not aware of that feature. :)

      Oh well, back to the drawing board...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-12-25 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls