What's new

Seemingly-undocumented C++ operators

BGNG

New member
I've been looking through a piece of code written in C++ that I consider to be particularly valuable, but I'm not sure what every syntaxual component means. I know quite a bit, but this is beyond my experience and I cannot find documentation on it anywhere.

I'm seeing the circumflex used in the ^ and ^= operators, but last time I checked, the exp() function was required to do exponents... What use does this symbol have in C++? (Example: x ^= y ^ z;)

I also see a vertical seperator used in the | operator, but from what I can tell, the OR operator is written as ||. What might | be? (Example: x = (y | z);)

Lastly, I'm finding use of a tilde in a numeric expression. I have no clue what this does. Anyone know? (Example: unsigned int x = ~0;)

Any guidence will be appreciated.
 
Last edited:

bcrew1375

New member
Those are all bit operators.

|| is for conditionals. It returns true or false

^ is Exclusive OR.
| is Inclusive OR.
~ is NOT.

These will return the actual value.

Example:
Code:
if ((variable1 == 0) || (variable2 == 0))
     /* Do stuff */
So, by this statement, if either variable1 or variable2 is off, the code will be executed.

Code:
variable1 = 3;
variable2 = 7;

variable1 |= variable2;
variable1 would equal 7.
 
Last edited:

smcd

Active member
last time I checked, the exp() function was required to do exponents...

that'd be pow not exp ;)

logical operators:
(condition1) && (condition2)
(condition1) || (condition2)
!(condition)

bitwise manipulation:
| = OR
& = AND
^ = XOR
~ = NEGATE/NOT/INVERT
<< = SHIFT left
>> = SHIFT right

(Example: unsigned int x = ~0; )

that'd be the same (assuming 8bit byte and 4byte int) as:
unsigned int x = 0xFFFFFFFF; // since ~ inverts bits

googling "C++ bitwise manipulation" uncovers some links, such as:
http://www.cprogramming.com/tutorial/bitwise.html
 
Last edited:
OP
BGNG

BGNG

New member
Heh... Yeah, pow()... I thought "exp()" looked kinda funny, but I just couldn't put my finger on it. (-:

Well, thanks guys. That's what I was looking for.
 

Top