views:

3969

answers:

8

I often use ($var & 1) in my code, which returns true if $var is an odd number and false if it's an even number.

Just dawned on me that I have no idea what "&" actually does. Anyone care to explain?

+3  A: 

This link covers the operation.

Cody Brocious
+4  A: 

That is the bitwise AND - here is the PHP manual page on it.

Andrew Hare
+12  A: 

& is binary and. If you have a binary value, and you and with another binary value, then the result will be the bitwise and of the two. An example:

  01101010
& 01011001
= 01001000

The rightmost bit is either a 1 (and in that case the number is an odd number) or it is a 0, in which case the number is even. If you & a number with 1, you only look at the least significant bit, and the if checks if the number is a 1 or a 0. As others have mentioned, look at the bitwise operators for info on how they work.

Marius
+5  A: 

This is also interesting to know about bitwise and php.

/**
 * regular
 */
echo (true && true); // 1
echo (true && false); // nothing

echo (true || false); // 1
echo (false || false); // nothing

echo (true xor false); // 1
echo (false xor false); // nothing

/**
 * bitwise
 */
echo (true & true); // 1
echo (true & false); // 0

echo (true | false); // 1
echo (false | false); // 0

echo (true ^ false); // 1
echo (false ^ false); // 0
Ólafur Waage
thomasrutter
"regular" is known as the logical and/or
Ryan Kearney
+1  A: 

Two operations which are fundamental to binary systems are OR and AND.

OR means 'if either A is on or B is on'. A real world example would be two switches in parallel. If either is allowing current through, then current passes through.

AND means 'if both A and B is on'. The real world example is two switches in series. Current will only pass through if both are allowing current through.

In a computer, these aren't physical switches but semiconductors, and their functionality are called logic gates. They do the same sorts of things as the switches - react to current or no current.

When applied to integers, every bit in one number is combined with every bit in the other number. So to understand the bitwise operators OR and AND, you need to convert the numbers to binary, then do the OR or AND operation on every pair of matching bits.

That is why:

00011011 (odd number)
AND
00000001 (& 1)
== 
00000001 (results in 1)

Whereas

00011010 (even number)
AND
00000001 (& 1)
==
00000000 (results in 0)

The (& 1) operation therefore compares the right-most bit to 1 using AND logic. All the other bits are effectively ignored because anything AND nothing is nothing.

Other fundamental operations to binary systems include NOT and XOR. NOT means 'if A is off' and is the only form of logic gate that takes only one signal or 'parameter' instead of two. XOR means 'if either A or B is on, but not both'. And then there are NAND, NOR, and NXOR, which are basically just NOT combined with AND, OR, and XOR, ie NAND means 'if A and B are not both on'.

In programming, the operator

& means AND,
| means OR, 
~ means NOT, and
^ means XOR.

The others can be made up by combining these, for example:

~ (a & b) is equivalent to a NAND operation
thomasrutter
+2  A: 

I know your question is about understanding the bitwise operator and the accepted answer explains it well. But for the example you give, I cannot help but recommending you use the modulo operator instead:

($var % 2) instead of ($var & 1)

Because it makes the intent clear that you are checking that the number is odd (not divisible by two), and it is more generic, so you can use ($var % 3) the same way and deduce how it works for any N.

Pat
+1  A: 

FWIW Caffeine, I just Googled this very question and the first link was to this page and the first response was you complaining about someone who couldn't seem to bother to search Google first.

Bryan
+1  A: 

In addition to the other answers, it's worth noting that

if(func1() && func2())

Will only call func1() if it returns false ("lazy evaluation"), whereas

if(func1() & func2())

Will call both functions regardless, but the truth tables for both will be the same.

(At least, in most languages this is true, I assume PHP is smart enough to optimize away the second function call too)

Mark
+1, this is very interesting and not known by everyone
bodom_lx
**Edit:** It will only call `func1()` if it returns *false*, not *true*, because `false` "and" anything (true or false) will still be false.
Mark