note 33250 deleted from ref.bc by aidan
| From: | aidan@php.net | Date: | Sat, 11 Sep 2004 12:59:24 +0000 |
| Subject: | note 33250 deleted from ref.bc by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76479@lists.php.net to get a copy of this message | ||
Note Submitter: postmaster at notreached-diemarketers dot net
----
One way to use the base conversion functions provided above is
to implement bitwise math. I give you the functions bcand, bcor,
bcxor, bcleftshift, and bcrightshift (NOTE: the functions are
compatible with Pulstar's base-conversion functions, not
Oliver's):
// MAX_BASE is the maximum base that can be represented in
// one byte on the host machine. On most modern systems, this
// value can be 256, but if there are still any systems with 7-bit
// bytes out there, you should use 128 for maximum
// portability.
define('MAX_BASE', 128);
//// INTERFACE ROUTINES:
// Bitwise AND
function bcand($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcand');
}
// Bitwise OR
function bcor($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcor');
}
// Bitwise XOR
function bcxor($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcxor');
}
// Left shift (<<)
function bcleftshift($num, $shift)
{
bcscale(0);
return bcmul($num, bcpow(2, $shift));
}
// Right shift (>>)
function bcrightshift($num, $shift)
{
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)
{
return $x & $y;
}
function _bcor($x, $y)
{
return $x | $y;
}
function _bcxor($x, $y)
{
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)
{
$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)
{
$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)
{
$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)
{
return dec2base($num, MAX_BASE);
}
// bin2bc - Reverse of bc2bin
function bin2bc($num)
{
return base2dec($num, MAX_BASE);
}