?
Solved

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

Posted on 2006-11-21
1
Medium Priority
?
201 Views
Last Modified: 2012-05-05
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
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question