Operators
An operator is a symbol that tells the compiler to perform
specific mathematical or logical operation.
C++ has six categories of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Misc Operators
Assignment Operators
1
Arithmetic Operators
Operator Description Example
+ Adds two operands 5 + 7 will give 12
Subtracts second operand from 4 - 2 will give 2
- the first 2 – 4 will give -2
* Multiplies both operands 2 * 7 will give 14
Divides numerator by de-
/ numerator 4 / 2 will give 2
Modulus Operator and remainder 7 % 5 will give 2
% of after an integer division 4 % 2 will give 0
2
Arithmetic Operators
Let’s consider the
following example:
3
Arithmetic Operators
However, some of these operators can be combined to produce
a different operation, eg ++ and ---
Operator Description Example
Increment operator: increases
++ integer value by one a++ will give 6
Decrement operator: decreases
-- integer value by one a-- will give 4
4
Arithmetic Operators
Let’s consider
the following
example:
5
Relational Operators
Operator Description
Checks if the values of two operands are equal or not, if yes then
== the condition becomes true.
Checks if the values of two operands are equal or not, if values are
!= not equal then the condition becomes true.
Checks if the value of the left operand is greater than the value of
> the the right operand, if yes then the condition becomes true.
Checks if the value of the left operand is less than the value of the
< right operand, if yes then the condition becomes true.
Checks if the value of the left operand is greater than or equal to
the value of the right operand, if yes then the condition becomes
>= true.
Checks if the value of the left operand is less than or equal to the
<= value of the right operand, if yes then the condition becomes true.
6
Relational Operators
Let’s consider the following example:
7
Logical Operators
C++ supports the three logical operators
*
• Not (!),
*
• And (&&)
*
• OR (||)
8
Logical Operators
Let’s consider the following example:
9
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if it exists in both
& operands.
| Binary OR Operator copies a bit if it exists in either operand.
Binary XOR Operator returns 1 if the bits of both operands at the
^ same position are different and returns 0 if they are the same
Binary Ones Complement Operator is unary and has the effect of
~ 'flipping' bits.
Binary Left Shift Operator. The left operands value is moved left
<< by the number of bits specified by the right operand.
Binary Right Shift Operator. The left operands value is moved
>> right by the number of bits specified by the right operand.
10
Bitwise Operators
Now let’s assume a=20 and b= 14 and they are both stored in an 8 bit register
a=0001 0100 b=0000 1110
To perform the bitwise operation you compare the bit position of the operands
Operation Outcome Decimal Value
0001 0100
(A & B) 0000 1110 4
0000 0100
0001 0100
(A | B) 0000 1110 30
0001 1110
0001 0100
(A ^ B) 0000 1110 26
0001 1010
0001 0100
(~A ) -21
1110 1011
0001 0100
A << 2 80
0101 0000
0001 0100
A >> 2 3
0000 0011
11
Bitwise Operators
Let’s consider the
following example:
12
Assignment Operator
The assignment operator can be combined with other operators. The table below
shows the various combinations.
Operator Description Example
Add AND assignment operator, It adds right operand
to the left operand and assign the result to left C += A is equivalent
+= operand. to C = C + A
Subtract AND assignment operator, It subtracts right
operand from the left operand and assign the result to C -= A is equivalent
-= left operand. to C = C – A
Multiply AND assignment operator, It multiplies right
operand with the left operand and assign the result to C *= A is equivalent
*= left operand. to C = C * A
13
Assignment Operator
Operator Description Example
Divide AND assignment operator, It divides left
operand with the right operand and assign the C /= A is equivalent to C = C /
/= result to left operand. A
Modulus AND assignment operator, It takes
modulus using two operands and assign the result C %= A is equivalent to C = C %
%= to left operand. A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
14
Assignment Operator
Operator Description Example
Bitwise exclusive OR and assignment
^= operator. C ^= 2 is same as C = C ^ 2
Bitwise inclusive OR and assignment
|= operator. C |= 2 is same as C = C | 2
15
Assignment Operator
Let’s consider the following
example
16
Misc Operators
C++ supports other operators and the table below shows other operators that C++ supports.
Operator Description
Cast Casting operators convert one data type to another.
Comma operator causes a sequence of operations to be performed. The
value of the entire comma expression is the value of the last expression of
, the comma-separated list.
Conditional operator (?). If Condition is true then it returns value of X
Condition ? X : Y otherwise returns value of Y.
& Pointer operator & returns the address of a variable.
* Pointer operator * is pointer to a variable.
sizeof operator returns the size of a variable. For example, sizeof(a),
Sizeof where ‘a’ is integer, and will return 4.
17
Misc Operators
Cast
A cast is a special
operator that
forces one data
type to be
converted into
another data
type.
18
Misc Operators
, (Comma)
The comma
operator (,) is used
to separate two or
more expressions
that are included
where only one
expression is
expected.
19
Misc Operators
Conditional ? : Operator
The conditional
operator evaluates an
expression returning a
value if that expression
is true and a different
one if the expression is
evaluated as false.
Its format is:
condition? Exp1 : Exp2;
20
Misc Operators
Sizeof Operator
It is a compile-time
operator that
determines the
size, in bytes, of a
variable or data
type
21
Misc Operators
& and * (Pointer Operator)
Reference Operator (&):
The & is a unary operator
that returns the memory
address of its operand.
Dereference Operator (*) :
The dereference operator *
returns the value of the
variable located at the
address specified by its
operand.
22
Precedence of Operator
Operator precedence determines how an operator is evaluated in an expression
when compared to other operator.
Level Precedence Operator Description Grouping
group
1 Scope :: scope qualifier Left-to-right
3 Postfix (unary) ++ -- postfix Left-to-right
increment /
decrement
() functional forms
[] subscript
. -> member access
23
Precedence of Operator
Level Precedence Operator Description Grouping
group
2 Prefix (unary) ++ -- prefix increment / Right-to-left
decrement
~! bitwise NOT / logical
NOT
+- unary prefix
&* reference / dereference
new delete allocation / deallocation
sizeof parameter pack
(type) C-style type-casting
24
Precedence of Operator
Level Precedence group Operato Description Grouping
r
4 Pointer-to-member .* ->* access pointer Left-to-right
5 Arithmetic */% multiply, divide, Left-to-right
modulo
6 Arithmetic +- addition, subtraction Left-to-right
7 Bitwise shift << >> shift left, shift right Left-to-right
8 Relational < > <= >= comparison Left-to-right
operators
9 Equality == != equality / inequality Left-to-right
10 And & bitwise AND Left-to-right
25
Precedence of Operator
Level Precedence Operator Description Grouping
group
11 Exclusive or ^ bitwise XOR Left-to-right
12 Inclusive or | bitwise OR Left-to-right
13 Conjunction && logical AND Left-to-right
14 Disjunction || logical OR Left-to-right
15 Assignment-level = *= /= %= += -= assignment / compound Right-to-left
expressions >>= <<= &= ^= |= assignment
?: conditional operator
16 Sequencing , comma separator Left-to-right
26
Basic Input/Output
The standard C++ library includes the header file iostream, where the
standard input and output stream objects are declared.
Standard Output (cout): By default, the standard output of a program is the
screen, and the C++ stream object defined to access it, is cout.
cout is used in conjunction with the insertion operator, which is written as <<
Standard Input (cin): The standard input device is usually the keyboard.
Handling the standard input in C++ is done by applying the overloaded
operator of extraction (>>) on the cin stream.
The operator must be followed by the variable that will store the data that is
going to be extracted from the stream.
27
Basic Input/Output
Let’s consider the
following example
28