|
Posted by ColdShine on 11/03/85 11:42
> //unsigned shift right
> function fillWithZeroes($a, $b)
> {
> $z = hexdec(80000000);
> if ($z & $a)
> {
> $a = ($a>>1);
> $a &= (~$z);
> $a |= 0x40000000;
> $a = ($a>>($b-1));
> }
> else
> {
> $a = ($a>>$b);
> }
> return $a;
> }
This should handle an arbitrary number of bits for $a, allowing $b to be
greater than the number of machine word bits.
// Assuming 32-bit (1 sign) integer length.
function fillWithZeroes($a, $b)
{
// In PHP you could use is_float($a), but I don't know
// the equivalent Perl function.
if ($a > 0x7FFFFFFF)
{
// It is some FP number, must emulate shift with division.
$d = exp(2, $b);
$a = round($a / $d);
// Return int if possible.
if ($a <= 0x7FFFFFFF)
$a = (int)$a;
}
else
$a >>= $b;
return $a;
}
This yelds correct results as long as FP numbers have enugh precision. This
is obviously NOT a safe assumption; yet if FP numbers have 53 + 1
significand bits (64-bits IEEE FP), this fillWithZeroes should be able to
handle numbers from 0 to 2^54 - 1.
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law
Navigation:
[Reply to this message]
|