>) function bcrightshift($num, $shift) { bc_extention_check(); bcscale(0); return bcdiv($num, bcpow(2, $shift)); } //// INTERNAL ROUTINES // These routines operate on only one byte. They are used to // implement _bcbitwise_internal. function _bcand($x, $y) { bc_extention_check(); return $x & $y; } function _bcor($x, $y) { bc_extention_check(); return $x | $y; } function _bcxor($x, $y) { bc_extention_check(); return $x ^ $y; } // _bcbitwise_internal - The majority of the code that implements // the bitwise functions bcand, bcor, and bcxor. // // arguments - $x and $y are the operands (in decimal format), // and $op is the name of one of the three // internal functions, _bcand, _bcor, or _bcxor. // // // see also - The interfaces to this function: bcand, bcor, // and bcxor function _bcbitwise_internal($x, $y, $op) { bc_extention_check(); $bx = bc2bin($x); $by = bc2bin($y); // Pad $bx and $by so that both are the same length. equalbinpad($bx, $by); $ix=0; $ret = ''; for($ix = 0; $ix < strlen($bx); $ix++) { $xd = substr($bx, $ix, 1); $yd = substr($by, $ix, 1); $ret .= call_user_func($op, $xd, $yd); } return bin2bc($ret); } // equalbinpad - Pad the operands on the most-significant end // so they have the same number of bytes. // // arguments - $x and $y, binary-format numbers (converted // from decimal format with bc2bin()), passed // by reference. // // notes - Both operands are modified by this function. function equalbinpad(&$x, &$y) { bc_extention_check(); $xlen = strlen($x); $ylen = strlen($y); $length = max($xlen, $ylen); fixedbinpad($x, $length); fixedbinpad($y, $length); } // fixedbinpad - Pad a binary number up to a certain length // // arguments - $num: The operand to be padded. // // - $length: The desired minimum length for // $num // // notes - $num is modified by this function. function fixedbinpad(&$num, $length) { bc_extention_check(); $pad = ''; for($ii = 0; $ii < $length-strlen($num); $ii++) { $pad .= bc2bin('0'); } $num = $pad . $num; } // bc2bin - Convert a decimal number to the internal // binary format used by this library. // // return value - The binary representation of $num. function bc2bin($num) { bc_extention_check(); return dec2base($num, MAX_BASE); } // bin2bc - Reverse of bc2bin function bin2bc($num) { bc_extention_check(); return base2dec($num, MAX_BASE); } #### Next... # pulstar at mail dot com # 19-Sep-2002 06:27 # A good use for BCMath functions: # The functions below can convert a number in any base (from 2 to 256) to its # decimal value and vice-versa. // convert a decimal value to any other base value function dec2base($dec,$base,$digits=FALSE) { bc_extention_check(); if($base<2 or $base>256) die("Invalid Base: ".$base); bcscale(0); $value=""; if(!$digits) $digits=digits($base); while($dec>$base-1) { $rest=bcmod($dec,$base); $dec=bcdiv($dec,$base); $value=$digits[$rest].$value; } $value=$digits[intval($dec)].$value; return (string) $value; } // convert another base value to its decimal value function base2dec($value,$base,$digits=FALSE) { bc_extention_check(); if($base<2 or $base>256) die("Invalid Base: ".$base); bcscale(0); if($base<37) $value=strtolower($value); if(!$digits) $digits=digits($base); $size=strlen($value); $dec="0"; for($loop=0;$loop<$size;$loop++) { $element=strpos($digits,$value[$loop]); $power=bcpow($base,$size-$loop-1); $dec=bcadd($dec,bcmul($element,$power)); } return (string) $dec; } function digits($base) { bc_extention_check(); if($base>64) { $digits=""; for($loop=0;$loop<256;$loop++) { $digits.=chr($loop); } } else { $digits ="0123456789abcdefghijklmnopqrstuvwxyz"; $digits.="ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; } $digits=substr($digits,0,$base); return (string) $digits; } # The purpose of digits() function above is to supply the characters that will # be used as digits for the base you want. NOTE: You can use any characters for # that when you convert to another base, but when you convert again to the # decimal base, you need to use the same characters or you will get another # unexpected result. ?>