x
Solved

# Please explain this piece of code: box.vector.x * ((i & 1) != 0 ? -1 : 1)

Posted on 2006-11-21
Medium Priority
211 Views
Hi all

I know what the piece of code does, and why it does it what I am really after is what:

((i & 1) != 0 ? -1 : 1)

is all about. I know it's a short hand conditional statement, but I'm really not sure at all what it does, or how it should really be used. Any help ay all would be greatly appreciated.

Cheers
0
Question by:directxBOB
1 Comment

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17993515
the expressions ((i & 1) != 0 ? -1 : 1
follows the following syntax:
<boolean condition>? <true_result> : <false_result>

if you know access:
iif(((i & 1) != 0 , -1 , 1)

i & 1   -> this returns the bitwise and operation between i and 1.
in practice, this means: check if the last bit is set to 1 or 0.
in english: is i odd or even.

hence, the expression means: return -1 if i is odd, return 1 if i is even
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.