note 60312 added to function.decbin
| From: | j dot preece at gmail dot com | Date: | Tue, 03 Jan 2006 13:00:02 +0000 |
| Subject: | note 60312 added to function.decbin | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101146@lists.php.net to get a copy of this message | ||
It occured to me there must be a simple way to produce binary numbers from decimal ones with any
number of bits, here's my attempt at a decimal to extended binary function. I hope the comments
make it clear:
<?php
function decextbin($decimalnumber,$bit)
{
/* decextbin function
by James Preece (j.preece@gmail.com)
http://www.lovingit.co.uk
Please feel free to use this function. If you find
if useful I would love to hear from you.
*/
/* Function to return a binary number with as many
bits as are requested...
This works on the following principal. A binary number
represents a figure 2,4,8,16 etc. If we work from the
top down we can determine if a number contains each figure
as a fraction and then work with what remains.
For example, if we wish to display the number 10 as a 4 bit
number we first discover the figure of the maximum bit by doubling
four times:
1 2 4 8
The maxiumum figure is 8.
Next we work down the figures:
8 goes in to 10 so the first digit is 1 leaving 2
4 does not go in to 2 so the second digit is 0
2 goes in to 2 so the third digit is 1
1 does not go in to 0 so the fourth digit is 0
Output: 1010
Now for the actual code!
First we find that maximum value represented by the
leftmost binary digit. For error checking purposes
we also calulate the maximum number we can display
using the number of bits requested: */
$maxval = 1;
$sumval = 1;
for($i=1;$i<$bit;$i++)
{
$maxval = $maxval * 2;
$sumval = $sumval + $maxval;
}
/* Using our sumval we now check if it is possible
to display the decimal number our function received: */
if ($sumval < $decimalnumber) return 'ERROR - Not enough bits to display this figure in
binary.';
/* Then we work down through the figures, to get a
better idea of how this works remove the commenting
from the echo lines */
for($bitvalue=$maxval;$bitvalue>=1;$bitvalue=$bitvalue/2)
{
//echo 'Bit Value: '.$bitvalue.'<br />';
//echo 'Decimal Number: '.$decimalnumber.'<br />';
if (($decimalnumber/$bitvalue) >= 1) $thisbit = 1; else $thisbit = 0;
//echo 'This Bit: '.$thisbit.'<br /><br />';
if ($thisbit == 1) $decimalnumber = $decimalnumber - $bitvalue;
$binarynumber .= $thisbit;
}
/* Finally we return the output... */
return $binarynumber;
}
?>
An example usage might be like so:
<?php
for ($i = 0;$i<=8;$i++)
{
echo decextbin($i,4).'<br />';
}
?>
Output:
0000
0001
0010
0011
0100
0101
0110
0111
1000
No need for padding or anything - also goes way over 32 bits. Huzzah. I suspect its limited by the
maximum string length in php. Hmmm - Not sure what that is.
----
Manual Page -- http://www.php.net/manual/en/function.decbin.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60312
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60312&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60312&report=yes
Search -- http://master.php.net/manage/user-notes.php