note 27438 added to function.decbin
| From: | matt at nexxmedia dot com | Date: | Fri, 06 Dec 2002 00:29:52 +0000 |
| Subject: | note 27438 added to function.decbin | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40394@lists.php.net to get a copy of this message | ||
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
--
http://www.php.net/manual/en/function.decbin.php
http://master.php.net/manage/user-notes.php?action=edit+27438
http://master.php.net/manage/user-notes.php?action=delete+27438
http://master.php.net/manage/user-notes.php?action=reject+27438