How to print binary representation (bits) of a number in PHP

2 Answers

0 votes
echo decbin(9) . "<br />";
echo decbin(13) . "<br />";
echo decbin(15) . "<br />";
echo decbin(20) . "<br />";

 
/*
run:

1001
1101
1111
10100

*/

 



answered Oct 10, 2016 by avibootz
0 votes
echo dec2bin(9) . "<br />";
echo dec2bin(13) . "<br />";
echo dec2bin(15) . "<br />";
echo dec2bin(20) . "<br />";

function dec2bin($dec, $pad = 8) 
{
    return str_pad(decbin($dec), $pad, "0", STR_PAD_LEFT);
}
 
/*
run:

00001001
00001101
00001111
00010100

*/

 



answered Oct 10, 2016 by avibootz
...