472,119 Members | 1,516 Online

# what is & 0x1 in this context?

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]);
Nov 14 '05 #1
5 19019
John wrote:
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)

--
Eric Enright /"\
ericAtiptsoftDcom \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Nov 14 '05 #2
agree

"John" <ia******@hotmail.com> Ð´ÈëÓÊ¼þ
news:wu********************@comcast.com...
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]);

Nov 14 '05 #3
John <ia******@hotmail.com> wrote:
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 (Zo**********@daimlerchrysler.com)
"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
Nov 14 '05 #4
"John" <ia******@hotmail.com> wrote in message news:<wu********************@comcast.com>...
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"
Nov 14 '05 #5
Yep, i did read the comment. And after hearing it, it does make sense. I
guess I was having a senior moment =)
"Zoran Cutura" <zo**********@daimlerchrysler.com> wrote in message
news:ca**********@news.sns-felb.debis.de...
John <ia******@hotmail.com> wrote:
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 (Zo**********@daimlerchrysler.com)
"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

Nov 14 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.