100% found this document useful (1 vote)
26 views13 pages

Operators in C++

The document provides an overview of operators in C++, categorizing them into arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. Each category is explained with examples, detailing how operators function and their usage in programming. Key operators include addition, subtraction, logical AND, and bitwise operations, among others.

Uploaded by

Sathis Rajive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
26 views13 pages

Operators in C++

The document provides an overview of operators in C++, categorizing them into arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. Each category is explained with examples, detailing how operators function and their usage in programming. Key operators include addition, subtraction, logical AND, and bitwise operations, among others.

Uploaded by

Sathis Rajive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

OPERATORS IN C++

Operators in C++
 An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. C++ is rich in
built-in operators and provide the following types of operators

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
ARITHMETIC OPERATORS

 There are following arithmetic operators supported by C++ language .


Assume variable A holds 10 and variable B holds 20.
Operat Description Example
or

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of after B % A will give 0


an integer division

++ Increment operator, increases integer A++ will give 11


value by one

-- Decrement operator, decreases integer A-- will give 9


value by one
Relational Operators

 There are following relational operators supported by C++


language.Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example

== Checks if the values of two operands are equal or not, if yes then (A == B) is not true.
condition becomes true.

!= Checks if the values of two operands are equal or not, if values (A != B) is true.
are not equal then condition becomes true.

> Checks if the value of left operand is greater than the value of (A > B) is not true.
right operand, if yes then condition becomes true.

< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal to the (A >= B) is not true.
value of right operand, if yes then condition becomes true.

<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
Logical Operators

 There are following logical operators supported by C++


language.Assume variable A holds 1 and variable B holds 0, then −

Operator Description Example

&& Called Logical AND operator. If both the operands are non-zero, (A && B) is false.
then condition becomes true.

|| Called Logical OR Operator. If any of the two operands is non- (A || B) is true.


zero, then condition becomes true.

! Called Logical NOT Operator. Use to reverses the logical state !(A && B) is true.
of its operand. If a condition is true, then Logical NOT operator
will make false.
Bitwise Operators

 Bitwise operator works on bits and perform bit-by-bit operation. The


truth tables for &, |, and ^ are as follows −

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1
Bitwise Operators
Assume if A = 60; and B = 13; now in binary format they will be as
follows −

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Bitwise Operators
Operator Description Example

& Binary AND Operator copies a bit to the result if it


(A & B) will give 12 which is 0000 1100
exists in both operands.

| Binary OR Operator copies a bit if it exists in either (A | B) will give 61 which is 0011 1101
operand.

^ Binary XOR Operator copies the bit if it is set in one


(A ^ B) will give 49 which is 0011 0001
operand but not both.

~ Binary Ones Complement Operator is unary and has (~A ) will give -61 which is 1100 0011 in
the effect of 'flipping' bits. 2's complement form due to a signed
binary number.

<< Binary Left Shift Operator. The left operands value is


moved left by the number of bits specified by the right A << 2 will give 240 which is 1111 0000
operand.

>> Binary Right Shift Operator. The left operands value is


moved right by the number of bits specified by the A >> 2 will give 15 which is 0000 1111
right operand.
Assignment Operators

Operato Description Example


r

= Simple assignment operator, Assigns


values from right side operands to C = A + B will assign value of
left side operand. A + B into C

+= Add AND assignment operator, It


adds right operand to the left C += A is equivalent to C = C
operand and assign the result to left + A
operand.

-= Subtract AND assignment operator,


It subtracts right operand from the C -= A is equivalent to C = C -
left operand and assign the result to A
left operand.

*= Multiply AND assignment operator, It


multiplies right operand with the left C *= A is equivalent to C = C *
operand and assign the result to left A
operand.
Assignment Operators

Operato Description Example


r

/= Divide AND assignment operator, It


divides left operand with the right C /= A is equivalent to C = C /
operand and assign the result to left A
operand.

%= Modulus AND assignment operator,


It takes modulus using two operands C %= A is equivalent to C = C
and assign the result 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
Assignment Operators

Operator Description Example

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= 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
Misc Operators

Sr.No Operator & Description

1 sizeof
sizeof operator returns the size of a variable. For example, sizeof(a), where ‘a’ is integer, and
will return 4.

2 Condition ? X : Y
Conditional operator (?) If Condition is true then it returns value of X otherwise returns
value of Y.

3 ,
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.

4 . (dot) and -> (arrow)


Member operators are used to reference individual members of classes, structures, and
unions.

5 Cast
Casting operators convert one data type to another. For example, int(2.2000) would return
2.

6 & Pointer operator & returns the address of a variable. For example &a; will give actual
address of the variable.

You might also like