Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see binary representation of variable

Tags:

php

Is there any possibility to see binary representation of variable?

like image 748
PaulP Avatar asked Apr 18 '11 15:04

PaulP


2 Answers

Like so:

echo decbin(3); // 11
like image 107
Josh Avatar answered Nov 12 '22 07:11

Josh


Another solution:

function d2b($dec, $n = 16) {
    return str_pad(decbin($dec), $n, "0", STR_PAD_LEFT);
}

Example:

// example:
echo d2b(E_ALL);
echo d2b(E_ALL | E_STRICT);
echo d2b(0xAA55);
echo d2b(5);

Output:
0111011111111111
0111111111111111
1010101001010101
0000000000000101
like image 7
Mario Campa Avatar answered Nov 12 '22 08:11

Mario Campa