Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is & in return($var & 1) in PHP [duplicate]

Tags:

Possible Duplicate:
Reference - What does this symbol mean in PHP?

When I was reading this php page, I was not sure what & is doing in $var & 1.

function odd($var) {     // returns whether the input integer is odd     return($var & 1); } 

Is it returning a reference? I am not sure.

If you can explain it or direct me a php page, I will appreciate it.

Thanks in advance.

like image 700
shin Avatar asked Nov 13 '12 09:11

shin


People also ask

What is love female singer?

"What Is Love" was written and produced by German music producer and composer Dee Dee Halligan (Dieter Lünstedt a.k.a. Tony Hendrik) and his partner/wife Junior Torello (Karin Hartmann-Eisenblätter a.k.a. Karin van Haaren) of Coconut Records in Hennef (Sieg) near Cologne.

What is Live Twice?

"Live Twice" is the title track and the second single from Scottish singer Darius's second album, Live Twice (2004). The song was released on 10 January 2005 as his sixth and final single. It peaked at number seven on the UK Singles Chart and number 24 in Ireland.

What is love twice concept?

According to JYP Entertainment, the song is about "the love girls would dream about or imagine after learning about it through books, movies or dramas" and it has a bright melody and uptempo dance beat incorporating trap.


2 Answers

It's a bitwise-AND operation. All odd numbers have LSB (least significant bit set to 1), even numbers - 0.

So it simply "ANDs" two numbers together. For example, 5. It is represented as 101 in binary. 101 & 001 = 001 => true, so it is odd.

like image 173
Dmitri Avatar answered Sep 23 '22 01:09

Dmitri


It is performing bitwise ANDing. That is a bitwise operator

$a & $b Bits that are set in both $a and $b are set.


In this case, return($var & 1); will do a bitwise AND against 0000....0001 returning 1 or 0 depending on the last bit of $var.

If the binary representation of a number ends in 0, it is even (in decimal).

If the binary representation of a number ends in 1, it is odd (in decimal).

like image 22
Anirudh Ramanathan Avatar answered Sep 26 '22 01:09

Anirudh Ramanathan