note 101678 added to function.bcmul
| From: | admin at spamhere dot sinfocol dot org | Date: | Tue, 04 Jan 2011 06:17:01 +0000 |
| Subject: | note 101678 added to function.bcmul | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175439@lists.php.net to get a copy of this message | ||
Well, I have a little problem implementing Blake Hash in my server because it is not a x64 server
machine. I made a little function that use the powerfull of BC library to do the bitwise operation
Shift.
<?php
echo 'Left Shift test<br />';
bprint('1', decbin(1));
bprint('1 << 32 (Fail)', decbin(1 << 32)); //Fail, operation not succesfull in
32-bit machine
bprint('shiftleft(1, 32) (Success)', dec2bin(shiftleft('1', '32')));
//decbin fails, so we use personalized function, success
echo '<br />';
echo 'Right Shift test<br />';
bprint('9223372036854775808', dec2bin('9223372036854775808'));
bprint('9223372036854775808 >> 63 (Fail)', decbin(9223372036854775808 >> 63));
bprint('rightshift(9223372036854775808, 63) (Success)',
decbin(rightshift('9223372036854775808', '63')));
function shiftleft($num, $bits) {
return bcmul($num, bcpow('2', $bits));
}
function rightshift($num, $bits) {
return bcdiv($num, bcpow('2', $bits));
}
function bprint($title, $content) {
echo $title . '<br />' . str_pad($content, 64, '0', STR_PAD_LEFT) .
'<br />' . PHP_EOL;
}
//http://www.php.net/manual/en/function.decbin.php#99533
function dec2bin($dec) {
// Better function for dec to bin. Support much bigger values, but doesnât support signs
for ($b = '', $r = $dec; $r >1;) {
$n = floor($r / 2);
$b = ($r - $n * 2) . $b;
$r = $n; // $r%2 is inaccurate when using bigger values (like 11.435.168.214)!
}
return ($r % 2) . $b;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.251.5.125 (proxied: 200.110.100.195)
----
Manual Page -- http://www.php.net/manual/en/function.bcmul.php
Edit -- https://master.php.net/note/edit/101678
Del: integrated -- https://master.php.net/note/delete/101678/integrated
Del: useless -- https://master.php.net/note/delete/101678/useless
Del: bad code -- https://master.php.net/note/delete/101678/bad+code
Del: spam -- https://master.php.net/note/delete/101678/spam
Del: non-english -- https://master.php.net/note/delete/101678/non-english
Del: in docs -- https://master.php.net/note/delete/101678/in+docs
Del: other reasons-- https://master.php.net/note/delete/101678
Reject -- https://master.php.net/note/reject/101678
Search -- https://master.php.net/manage/user-notes.php