note 27438 deleted from function.decbin by danielc
| From: | danielc@php.net | Date: | Mon, 27 Apr 2009 02:11:16 +0000 |
| Subject: | note 27438 deleted from function.decbin by danielc | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153796@lists.php.net to get a copy of this message | ||
Note Submitter: matt at nexxmedia dot com
Reason: 2
----
A quick one-line bitmask. Great for storing a list of checkbox settings in a MySQL database. This
one works for TinyINT (8 bits). Note: I had to make it two lines here so it would fit.
$decval = 83;
/*
decimal to binary, padded with 0s, reversed so the least significant digit is first, split with
colons, and then separated into an array
*/
$binstring = strrev(str_pad(decbin ($decval),8,"0",STR_PAD_LEFT))
$bitarray = explode(":",chunk_split($binstring, 1, ":"));
for($x = 0; $x < 8; $x++) {
echo $bitarray[$x]." ";
}
// will output 1 1 0 0 1 0 1 0