0% found this document useful (0 votes)
5 views

2 Operators

The document discusses different types of operators in C++ including arithmetic, relational, logical, bitwise, assignment and other operators. It explains each operator in detail along with examples and precedence rules.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2 Operators

The document discusses different types of operators in C++ including arithmetic, relational, logical, bitwise, assignment and other operators. It explains each operator in detail along with examples and precedence rules.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Operators

• An operator is a symbol that directs the computer to perform certain mathematical or


logical manipulations and is usually used to manipulate data and variables.
• For example: +, *, -, /, <, >
• C++ language is rich in built-in operators and provides the following types of operators:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
Arithmetic operators
• An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and
variables).
Relational operators
• A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.
Logical operators
• An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false.
Bitwise operators

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


Assume A = 60 and B = 13 in binary format, they will be as follows:
A = 0011 1100 B = 0000 1101

Operator Description Example


& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
~ Binary One's Complement Operator is unary and has (~A ) = ~(60), i.e,. -0111101
the effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the A << 2 = 240 i.e., 1111 0000
right operand.

>> Binary Right Shift Operator. The left operands value


is moved right by the number of bits specified by the A >> 2 = 15 i.e., 0000 1111
right operand.
Assignment operator
• An assignment operator is used for assigning a value to a variable.
Misc operators
Operator Description Example

sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

Returns the address of a


& &a; returns the actual address of the variable.
variable.

* Pointer to a variable. *a;

If Condition is true ? then value X :


?: Conditional Expression.
otherwise value Y
Operator precedence in C++
• Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others. For example: the multiplication operator has a
higher precedence than the addition operator.
• For example: x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator
* has a higher precedence than +, so it first gets multiplied with 3*2 and
then adds into 7.
• Here, operators with the highest precedence appear at the top of the table,
those with the lowest appear at the bottom. Within an expression, higher
precedence operators will be evaluated first.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Highest Priority
Unary + - ! ~ ++ - - (type)* & Right to left
sizeof
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= Right to left
<<= &= ^= |=
Comma , Left to right
Lowest Priority
Increment and Decrement operators

• C++ has two operators to change the value of the operand by 1. These
operators are:
Increment operator (++): it increments the value of operand by 1
Decrement operator (--): it decreases the value of the operand by 1
Pre-increment
• A pre-increment operator is used to increment the value of a variable before
using it in a expression that is first value is incremented and then used
inside the expression.
• Syntax: a = ++x;
• Example: if x = 10;
a = ++x;
then a = 11
Because the value of ‘x’ is modified before assigning it to ‘a’
Post-increment
• A post increment operator is used to increment the value of the variable
after executing expression completely in which post increment is used.
• Syntax: a = x++;
• Example: if x = 10;
a = x++;
then a = 10
Because the value of ‘x’ is modified after assigning it to ‘a’
Pre decrement and Post decrement

• Pre decrement and post decrement works same as the pre increment and
post increment respectively.
• The only difference is that it is used to decrease the value of the operand by
1.

You might also like