Solved

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

Posted on 2006-11-21
1
198 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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