&
is the bitwise AND operator (not to be confused with &&
, which is a comparison operator)! &
compares the thing on its left and the thing on its right, bit by bit, and only outputs a 1
for each bit where BOTH inputs bits had a 1
. I'll show an example:
In the following example, the top number is pow(2, $power)
, where $power = 3
. It's written in binary form to make it clearer: doing 2 to the power of 3 has shifted a binary 1
right 3 places.
00001000 <- this is pow(2, 3)
& 01101010 <- this is your $decimal (in binary form)
========
= 00001000
You see? All the output bits are 0, except where BOTH input bits were 1.
What we have done is we have used pow(2, 3)
as a MASK: it has cleared EVERY bit to 0, except the one we are interested in (bit 3). That bit will be 1 if $decimal
contains this particular power of 2, otherwise it will be 0:
$decimal contains that power of 2 -> result will be 00001000
$decimal does not contain that ^2 -> result will be 00000000
Since any non-zero integer evaluates to true
in PHP, the if
will occur if that bit is set (i.e. $decimal
contains that power of 2), and the else
will occur if that bit is not set (because the &
will result in an output of 00000000
).
Please comment if you need any of that clarified. Hope it helps.