note 73282 added to function.decbin
| From: | darkshad3atyahoodotcom at osu1 dot php dot net | Date: | Thu, 15 Feb 2007 23:15:19 +0000 |
| Subject: | note 73282 added to function.decbin | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123470@lists.php.net to get a copy of this message | ||
<?php
Print bindecValues("1023");
function bindecValues($decimal, $reverse=false, $inverse=false) {
/*
1. This function takes a decimal, converts it to binary and returns the
decimal values of each individual binary value (a 1) in the binary string.
You can use larger decimal values if you pass them to the function as a string!
2. The second optional parameter reverses the output.
3. The third optional parameter inverses the binary string, eg 101 becomes 010.
-- darkshad3 at yahoo dot com
*/
$bin = decbin($decimal);
if ($inverse) {
$bin = str_replace("0", "x", $bin);
$bin = str_replace("1", "0", $bin);
$bin = str_replace("x", "1", $bin);
}
$total = strlen($bin);
$stock = array();
for ($i = 0; $i < $total; $i++) {
if ($bin{$i} != 0) {
$bin_2 = str_pad($bin{$i}, $total - $i, 0);
array_push($stock, bindec($bin_2));
}
}
$reverse ? rsort($stock):sort($stock);
return implode(", ", $stock);
}
?>
The printed result is : 1, 2, 4, 8, 16, 32, 64, 128, 256, 512
----
Server IP: 193.190.198.20
Probable Submitter: 62.235.142.39
----
Manual Page -- http://www.php.net/manual/en/function.decbin.php
Edit -- https://master.php.net/note/edit/73282
Del: integrated -- https://master.php.net/note/delete/73282/integrated
Del: useless -- https://master.php.net/note/delete/73282/useless
Del: bad code -- https://master.php.net/note/delete/73282/bad+code
Del: spam -- https://master.php.net/note/delete/73282/spam
Del: non-english -- https://master.php.net/note/delete/73282/non-english
Del: in docs -- https://master.php.net/note/delete/73282/in+docs
Del: other reasons-- https://master.php.net/note/delete/73282
Reject -- https://master.php.net/note/reject/73282
Search -- https://master.php.net/manage/user-notes.php