Quick Search
»

# C++ Operators

Modified on Sat, Jan 24, 2009, 7:14 PM Categorized as C and C-plus-plus

### Unary¶

Name Syntax Return Value
Increment ++A A + 1
A++ A
Decrement ––A A - 1
A-- A

### Binary¶

Name Syntax Assignment
Shortcut
Arithmetic A + B
A - B
A * B
A / B
Yes
Modulo (remainder) A % B Yes
Logical AND,
OR,
NOT
A && B
A || B
A ! B
No
Bitwise AND,
OR,
NOT,
XOR
A & B
A | B
A ~ B
A ^ B
Yes
Bit Shift Left,
Right
num << n
num >> n
Yes
Comparison A < B
A > B
A <= B
A >= B
A == B
A != B
No

#### Notes¶

• Assignment Shortcuts - For those operators which permit assignment shortcuts, the syntax LHS op= RHS is equivalent to LHS = LHS op RHS. For example, the following two statements are equivalent: A += 5 and A = A + 5

• Bit Shifts - When shifting bits left, zeros are padded on the right. When shifting bits right, the left-most bit is a sign bit, and is propagated to the right.

• Swapping integer values - To swap integer values without using an intermediate variable, use the following code: a ^= b; b ^= a; a ^= b;.

### Ternary¶

Name Syntax
Conditional condition ? ResultIfTrue : ResultIfFalse

### Precedence Order¶

Operators Usage Associativity
:: Scope Resolution Left
() [] -> .  Left
! ~ + (unary) - (unary) ++ –– & (unary) Logical NOT; Bitwise NOT; Arithmetic sign; Increment; Decrement; AddressOf Right
* (unary) (typecast) static_cast Pointer dereferencing; casting Right
const_cast dynamic_cast reinterpret_cast Casting Right
sizeof new delete...typeid  Right
.* (unary) ->*  Left
* / % Arithmetic Left
+ - Arithmetic Left
<< >> Bitwise shift Left
< <= > >= Logical Comparison Left
== != Logical Comparison Left
& Bitwise logic Left
^ Bitwise logic Left
| Bitwise logic Left
&& Boolean Logic Left
|| Boolean Logic Left
?: Conditional operator Right
= *= /= %= += -= Assigment; Assignment shortcuts Right
&= ^= |= <<= >>= Assignment shortcuts Right
, (comma) Multiple statements Left

#### Notes¶

If there are no parenthesis in an expression, operators of equal precedence are executed in a sequence determined by their associativity. For operators with an associativity of left, the left-most operator is executed first; for operators with an associativity of right, the right-most operator is executed first.

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2021, Patrick Jasinski.