tags:

views:

89

answers:

5

The variables values are listed below

$v['flag'] = 10
kPOSTAGE_HOME = 8

So what the heck does the following line do?!

if(($v['flag']&kPOSTAGE_HOME)==kPOSTAGE_HOME) {
    //do something
}
+4  A: 

It checks whether the bit-pattern in $v['flag'] has it's 3rd bit set.

xtofl
A: 

It checks whenever third bit is on in $v['flag']. The & is "bitwise and" operator, binary of 8 is "00000100", therefore then you will do "bitwise and" all bits except the third will be zero, so in case third bit is on it will remains, therefore you have further check for equality.

Artem Barger
+1  A: 

It's masking the '8' bit in the variable. The number '10' in base 10 == 1001 in binary, and 8 == 1000. So this means "does 1001 have the 1000" bit set?" The answer is 'yes'.

Peter Rowell
+6  A: 

& sets the bits set on both values. Some binary maths:

  00001010 | 10
& 00001000 |  8
---------------
= 00001000 |  8

So 10&8 returns 8, and 8==8. Reason is to check whether a flag in that bit mask is set ...

johannes
+2  A: 

And, for better readability, it can be simplified to the following:

if ( $v['flag'] & kPOSTAGE_HOME ) {
Derek Illchuk