mattdeans has asked for the wisdom of the Perl Monks concerning the following question:

hi guys
How to do 'unsigned shift right(java:>>>)' in perl?
  • Comment on How to do 'unsigned shift right' in perl?

Replies are listed 'Best First'.
Re: How to do 'unsigned shift right' in perl?
by davido (Cardinal) on Sep 02, 2011 at 05:53 UTC

    Have you met the Perl documentation yet? perlop is where you need to look for information on operators.

    Shift Operators (within perlop):

    Binary "<<" returns the value of its left argument shifted left by the number of bits specified by the right argument. Arguments should be integers. (See also Integer Arithmetic.)

    Binary ">>" returns the value of its left argument shifted right by the number of bits specified by the right argument. Arguments should be integers. (See also Integer Arithmetic.)

    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, else unsigned C integers are used. Either way, the implementation isn't going to generate results larger than the size of the integer type Perl was built with (32 bits or 64 bits).

    The result of overflowing the range of the integers is undefined because it is undefined also in C. In other words, using 32-bit integers, 1 << 32 is undefined. Shifting by a negative number of bits is also undefined.

    See also: use integer;. The integer pragma can be lexically scoped, so you're not committed to integer-only math throughout the script.


    Dave

Re: How to do 'unsigned shift right' in perl?
by BrowserUk (Patriarch) on Sep 02, 2011 at 07:58 UTC

    Chose your behaviour:

    [0] Perl> $n = 2**32; print $n; print $n >>=1 for 1 .. 32;; 4294967296 2147483648 1073741824 536870912 268435456 134217728 67108864 33554432 16777216 8388608 4194304 2097152 1048576 524288 262144 131072 65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 [0] Perl> $n = -2**32; print $n; print $n >>=1 for 1 .. 32;; -4294967296 9223372034707292160 4611686017353646080 2305843008676823040 1152921504338411520 576460752169205760 288230376084602880 144115188042301440 72057594021150720 36028797010575360 18014398505287680 9007199252643840 4503599626321920 2251799813160960 1125899906580480 562949953290240 281474976645120 140737488322560 70368744161280 35184372080640 17592186040320 8796093020160 4398046510080 2199023255040 1099511627520 549755813760 274877906880 137438953440 68719476720 34359738360 17179869180 8589934590 4294967295 [0] Perl> { use integer; $n = -2**32; print $n; print $n >>=1 for 1 .. + 32 };; -4294967296 -2147483648 -1073741824 -536870912 -268435456 -134217728 -67108864 -33554432 -16777216 -8388608 -4194304 -2097152 -1048576 -524288 -262144 -131072 -65536 -32768 -16384 -8192 -4096 -2048 -1024 -512 -256 -128 -64 -32 -16 -8 -4 -2 -1 [0] Perl>

    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.