Operators

Operators are used in C++ to perform desired operations on particular values or variables.

C++ operators are classified into the following categories:

• Arithmetic operators

• Assignment operators

• Relational operators

• Logical operators

• Bitwise operators

The order of priority given to these operators are:

Assignment > Arithmetic > Relational > Logical > Bitwise

Arithmetic Operators:

Operator

Name

Description

Example

+

x + y

-

Subtraction

Subtracts one value from another

x - y

*

Multiplication

Multiplies two values

x * y

/

Division

Divides one value from another

x / y

%

Modulus

Returns the division remainder

x % y

++

Increment

Increases the value of a variable by 1

++x

--

Decrement

Decreases the value of a variable by 1

--x

Assignment Operators

These are used to assign values to variables. Often the short hand properties are used to simplify the code.

Short Hand  Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

- =

x - = 3

x = x - 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3

Relational Operators

These are used to compare to values. These return values as either true (1) or false (0)

Operator

Name

Description

Example

&&

Logical AND

Returns 1 only if both statements are true

x=5 && x < 10

||

Logical OR

Returns 1 if any one of the statements is true

x < 5 || x < 8

!

Logical NOT

Inverts the given result. Output is 1 for false and 0 for true.

!(x < 5 && x < 10)

Bitwise Operator

Steps to performing Bitwise Operation

1. Convert the numbers into Binary

2. Perform Operation

3. Convert answer into Decimal

Operator

Name

Description

Example

&

Bitwise AND

Performs AND in binary

5&7=5

|

Bitwise OR

Perform OR in binary

5|7=7

^

Bitwise XOR

Performs XOR in binary

5^7=2

<<

Left shift

Shifts one bit to the left

6<<=12

>>

Right shift

Shifts one bit to the right

6>>=3

Note:(shortcut for left and right shift)

For left shift multiply the number by 2 for right shift divide the number by 2