Re: & behaves differently
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: & behaves differently

From: ColdShine <coldshine_nounderscores_@hotmail.com>
Date: Sun Mar 05 2006 - 23:46:25 CET

> //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 
Received on Mon May 1 02:28:08 2006