http://www.perlmonks.org?node_id=1030002


in reply to Implement Arithmetic Shift Right for signed number in perl

What is wrong with
printf '0x%x', -0xaaaaaaaa >> 32;
It gives the expected result.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Implement Arithmetic Shift Right for signed number in perl
by IamAwesom3 (Initiate) on Apr 23, 2013 at 15:15 UTC
    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