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
Solved

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

Posted on 2006-11-21
1
197 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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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