Home > SparkCharts > Cs > C++ > Operators

C++


 
 

Operators

 

Assignment Operator

 

a = b;

 

Assigns the value of b to a. Assignments are done from right to left, which means that a = b = c; will give you a case where a and b both have the same value as c.



 
 

Comparison Operators

 

a == b

 

Evaluates to true if a equals b.

 

a < b

 

Evaluates to true if a is less than b.

 

a > b

 

Evaluates to true if a is greater than b.

 

a != b or a not_eq b

 

Evaluates to true if a is not equal to b.

 

a <= b

 

Evaluates to true if a is less than or equal to b.

 

a >= b

 

Evaluates to true if a is greater than or equal to b

 

!a or not a

 

! is the not operator. It changes 0 to 1 and all other values to 0, which equates to true becoming false and false becoming true.

 

a && b or a and b

 

And operator. If both a and b are true then the expression evaluates to true. Otherwise it evaluates to false.

 

a || b or a or b

 

Or operator. Evaluates to true if either a or b is true. If both are false it evaluates to false.



 
 

Arithmetic operators

 

a + b

 

Addition

 

a – b

 

Subtraction

 

a * b

 

Multiplication

 

a / b

 

Division

 

a % b

 

Modulus (integer remainder)

 

a += b

 

Addition and assignment (a = a + b)

 

a -= b

 

Subtraction and assignment (a = a – b)

 

a *= b

 

Multiplication and assignment (a = a * b)

 

a /= b

 

Division and assignment (a = a / b)



 
 

Increment and Decrement Operators

 

a++

 

Returns value of a, then increments a; also known as pre-fix increment.

 

++a

 

Increments a, and then returns the incremented value of a; also known as post-fix increment.

 

a--

 

Returns value of a, then decrements a; also known as pre-fix decrement.

 

--a

 

Decrements a, and then returns the incremented value of a; also known as post-fix decrement.



 
 

Bitwise Operators

 

a & b or a bitand b

 

Bitwise and

 

a | b or a bitor b

 

Bitwise or

 

a ^ b or a xor b

 

Bitwise xor

 

a &= b or a and_eq b

 

Bitwise and combined with assignment (a = a & b)

 

a |= b or a or_eq b

 

Bitwise or combined with assignment (a = a | b)

 

a ^= b or a xor_eq b

 

Bitwise xor combined with assignment (a = a ^ b)

 

~a or compl a

 

Bitwise not (also known as complement)

 

a << b

 

Bitwise left shift. Bits shifted off the left end are lost and 0’s are inserted into the vacated bits on the right

 

a >> b

 

Bitwise right shift. Bits shifted off the right end are lost and 0’s are inserted into the vacated bits on the left



“TO ERR IS HUMAN, BUT TO REALLY FOUL THINGS UP REQUIRES A COMPUTER.”
            —ANONYMOUS