what is & 0x1 in this context?

J

John

I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);
 
E

Eric Enright

John said:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);

It's doing a bitwise AND on the least-significant bit of
"length". If it returns true (ie, that bit is set), then the
number is odd. Otherwise, it's even.

For example:

(5) 0101 & 0001 == 1 (odd, true)
(6) 0110 & 0001 == 0 (even, false)
 
Z

Zoran Cutura

John said:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);

Well it's the same as (lenght & 1) which will be true when
the least significant bit of the value of length is set. That
means the value in length is odd.

Did you read the comment above the code?
 
E

Evangelista Sami

John said:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);

"length & 0x1" get the left most bit of length so it is equivalent to
"length % 2"
 
J

John

Yep, i did read the comment. And after hearing it, it does make sense. I
guess I was having a senior moment =)


Zoran Cutura said:
John said:
I came across this nugget in my intarweb travels. It's part of a
median-calculation. Can someone tell me what the if( length & 0x1 ) is
doing?

Thanks!

---------------------------------------------
/* Is vector length odd or even ? */
if (length & 0x1)
ans = pCopy[length/2];
else
ans = .5 * (pCopy[length/2 - 1] + pCopy[length/2]);

Well it's the same as (lenght & 1) which will be true when
the least significant bit of the value of length is set. That
means the value in length is odd.

Did you read the comment above the code?


--
Z ([email protected])
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top