in reply to Implementing a signed/sign-propagating right bitwise shift
You could eliminate the loop pretty easily.
#!/usr/bin/perl use strict; use warnings 'all'; use Config; my $longsize = $Config{longsize}; my $bits = $longsize * 8; sub rshift { # Should be signed/sign-propagating right shift my ($x, $y) = @_; my $mask = 0; my $z = $x >> $y; $mask = (2 ** ($y + 1) -1) << ($bits - $y) if $x < 0; return $z | $mask; }
It won't be very easy to turn it into an infix operator in Perl 5 without using a source filter... and you really don't want to go there if you don't have to.
UPDATE:Or if you want to calculate native size a different way:
#!/usr/bin/perl use strict; use warnings 'all'; my $bits; { my $native = -1; $bits++ while $native >>= 1; } sub rshift { # Should be signed/sign-propagating right shift my ($x, $y) = @_; my $mask = 0; my $z = $x >> $y; $mask = ( -1 >> ($bits - $y) ) << ($bits - $y) if $x < 0; return $z | $mask; }
|
|---|