http://qs321.pair.com?node_id=1030000

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

Hello monks, I am trying to implement Arithmetic Shift Right for signed number in perl. This is how it works. $Ra = "0xAAAAAAAA"; then if I perform ASR 32 times the final value should be 0xFFFFFFFF. It copies sign bit from bit 31 to bit 31 and also to bit 30 and bit 30's data to bit 29 and so on. I read it online that if I use "use integer" in same scope as the shift operand then It will perform ASR. I wrote a code like this
my $Rm = "0xAAAAAAAA"; use integer; my $Rd = hex($Rm) >> 31; printf "Rd = 0x%x\t Rb = 0x%x\n",$Rd, hex($Rm);
Output is
Rd = 0x1 Rb = 0xaaaaaaaa
I am expecting Rd to be 0xFFFFFFFF but it doesn't seems to be working.

Can someone please help me?

Thanks P

Replies are listed 'Best First'.
Re: Implement Arithmetic Shift Right for signed number in perl
by Krambambuli (Curate) on Apr 23, 2013 at 08:36 UTC
    Maybe the following helps a bit clarifying what's what:

    #!/usr/bin/perl use strict; use warnings; use Data::Hexdumper qw(hexdump); use integer; my $Rm = '0xAAAAAAAA'; my $Rb = hex $Rm; my $Rc = pack 'l', $Rb; my $Rd = unpack 'l', $Rc; my $Re = $Rd >> 31; my $Rx = pack 'l', $Re; print 'Rm: ', hexdump( $Rm, { suppress_warnings => 1 } ); print 'Rb: ', hexdump( $Rb, { suppress_warnings => 1 } ); print 'Rc: ', hexdump( $Rc, { suppress_warnings => 1 } ); print 'Rd: ', hexdump( $Rd, { suppress_warnings => 1 } ); print 'Re: ', hexdump( $Re, { suppress_warnings => 1 } ); print 'Rx: ', hexdump( $Rx, { suppress_warnings => 1 } ); printf "Rb = 0x%x\t Re = 0x%x\n",$Rb, $Re;
    which displays
    Rm: 0x0000 : 30 78 41 41 41 41 41 41 41 41 00 00 00 00 00 00 : 0xAAA +AAAAA...... Rb: 0x0000 : 32 38 36 33 33 31 31 35 33 30 00 00 00 00 00 00 : 28633 +11530...... Rc: 0x0000 : AA AA AA AA 00 00 00 00 00 00 00 00 00 00 00 00 : ..... +........... Rd: 0x0000 : 2D 31 34 33 31 36 35 35 37 36 36 00 00 00 00 00 : -1431 +655766..... Re: 0x0000 : 2D 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : -1... +........... Rx: 0x0000 : FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 : ..... +........... Rb = 0xaaaaaaaa Re = 0xffffffffffffffff

    Krambambuli
    ---
Re: Implement Arithmetic Shift Right for signed number in perl
by choroba (Cardinal) on Apr 22, 2013 at 23:59 UTC
    What is wrong with
    printf '0x%x', -0xaaaaaaaa >> 32;
    It gives the expected result.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      That's not the correct way, It will not be true every time, this time you knew the result. I figured out the correct way
      my $Rm = 0xAAAAAAAA; my $Rd = $Rm >> 31; # any shift amount from 0 top 31 for 32 bit data. $Rd |= 0xffffffff << (32 - 31); #it can be (32 - any shift amount) $Rd = $Rd & 0xffffffff; #mask bits more than 32 printf "Rd = 0x%x\t Rb = %x\n",$Rd, $Rm;
      Output
      Rd = 0xffffffff Rb = 0xaaaaaaaa