KutePHP
KutePHP

Reputation: 2236

How & works in 7 & 1 in php

Can anybody help me to understand, how the following code works ? I know it will return 1 if for odd number and 0 for even number.

echo (7 & 1);  // result 1
echo (6 & 1);  // result 0

I think the numbers are converted to its binary. Please correct if I'm incorrect.

Upvotes: 2

Views: 101

Answers (2)

Akash
Akash

Reputation: 5012

Yes, you are performing a AND operation on the numbers, so

Dec     BINARY   Output
7  ===  0111
1  ===  0001
------------------------
AND  op 0001     1


Dec     BINARY
6  ===  0110
1  ===  0001
------------------------
AND  op 0000     0

Like Wise,

Dec     BINARY
7  ===  0111
6  ===  0110
------------------------
AND  op 0110         6

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

7 = 0000111b
1 = 0000001b
------------
& = 0000001b = 1

And for 6:

6 = 0000110b
1 = 0000001b
------------
& = 0000000b = 0

Upvotes: 5

Related Questions