Solved

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

Posted on 2006-11-21
1
195 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now