You are invited to Log in or Register a free Frihost Account!

# C++ code

amirkpe
HI
what this piece of code do :
if (getCurrentCycle() & 1 ) {
.........
}

'getCurrentCycle' returns an integer between 0 - 6000

thx
BlackSkad
if I'm right, that condition checks if the current cycle is odd or not.

The &-operator works on binairy level. It's the bitwise AND (so 1 + 1 = 1, 0 + 1 = 0,...) When you do something like that condition, the result will be 1 or 0.

An exemple: getCurrentCycle returns 3, which is 0011 in binairy. 1 written in binairy is 0001. When you perform a bitwise AND, you'll get this (read vertically):
 Code: 0011 0001 ---- 0001 => which is 1 and says the original number was odd.

With getCurrentCycle = 4 (0100), you'll get this:
 Code: 0100 0001 ---- 0000 => which is zero and says the original number was not odd.

So the expression (getCurrentCycle() & 1) returns 0 or 1, which are the two common numbers for false and true. The expression evaluates true when getCurrentCycle is odd, else it will be false.

I hope you understand my explanation.
Greatz,
BlackSkad
raki_sg
Well the explainationn provided is exactly right..
"&" operator is the bitwise AND.

I would like to know more about Bitwise operator does anyone know
of good examples...

Showing:
Bitwise OR, BitShift Operator ( Shift Left, Shift Right )
I would appreciate it.

BlackSkad wrote:
if I'm right, that condition checks if the current cycle is odd or not.

The &-operator works on binairy level. It's the bitwise AND (so 1 + 1 = 1, 0 + 1 = 0,...) When you do something like that condition, the result will be 1 or 0.

An exemple: getCurrentCycle returns 3, which is 0011 in binairy. 1 written in binairy is 0001. When you perform a bitwise AND, you'll get this (read vertically):
 Code: 0011 0001 ---- 0001 => which is 1 and says the original number was odd.

With getCurrentCycle = 4 (0100), you'll get this:
 Code: 0100 0001 ---- 0000 => which is zero and says the original number was not odd.

So the expression (getCurrentCycle() & 1) returns 0 or 1, which are the two common numbers for false and true. The expression evaluates true when getCurrentCycle is odd, else it will be false.

I hope you understand my explanation.
Greatz,
BlackSkad
BlackSkad
I learned c++ from an online book of Bruce Eckel. Had to search it for a while (website wasn't very clear too) but here's a link to download the two parts:
Download page It might be a little outdated on some topics (2001-2003), but it was a good start for me.

The chapter you need is in "Thinking in C++ 2nd Edition, Volume 1". I thought it was chapter 3, but I'm not sure.

Just some examples.
Bitwise OR: is always 1, except when both bits are 0
 Code: 1001 0101 ---- 1101

Bitwise NOT is a unairy operator. It turns a 1 into 0 and vice versa
 Code: 1001 ---- 0110

Bitwise shiftoperator: exists in left- and right-version (1 << x or 1 >> x). This moves every bit x positions to the left/right.
 Code: 1 << 2 0001 << 2 ---- 0100 (= 4)

I hope this little examples were usefull

Greatz,
BlackSkad

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.