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

4 - C++ Operators PDF

C++ has many operators for arithmetic, comparison, logic, assignment, increment/decrement, conditional evaluation, and bitwise operations. Some key operators include arithmetic operators for basic math, relational operators for comparisons, logical operators for AND/OR/NOT logic, and assignment operators for storing values. Bitwise operators allow modifying variables at the bit level using operations like AND, OR, XOR, and shifting bits left or right.

Uploaded by

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

4 - C++ Operators PDF

C++ has many operators for arithmetic, comparison, logic, assignment, increment/decrement, conditional evaluation, and bitwise operations. Some key operators include arithmetic operators for basic math, relational operators for comparisons, logical operators for AND/OR/NOT logic, and assignment operators for storing values. Bitwise operators allow modifying variables at the bit level using operations like AND, OR, XOR, and shifting bits left or right.

Uploaded by

Richa Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C++ Operators

Computer Programming (Object Oriented Programming)


CSN104
C++ Operators
1. Arithmetic operators (+, - , *, /, %)
2. Relational operators (<, <=, >, >=, ==, !=)
3. Logical operators (&&, ||, !)
4. Assignment operators (+=, -=, *=, /=)
5. Increment and decrement operators (++, --)
6. Conditional operators (?:)
7. Bitwise operators (&, |, ^, <<, >>)
8. Special operators ()
Arithmetic Operators
Operator example Meaning

+ a+b Addition

- a–b Subtraction

* a*b Multiplication

/ a/b Division

% a%b Modulo division- remainder


Relational and comparison operators
( ==, !=, >, <, >=, <= )

operator description
== Equal to
!= Not equal to (7 == 5) // evaluates to false
< Less than (5 > 4) // evaluates to true
(3 != 2) // evaluates to true
> Greater than (6 >= 6) // evaluates to true
(5 < 5) // evaluates to false
<= Less than or equal to
>= Greater than or equal to
Let a=2, b=3 and c=6

(a == 5) // evaluates to false, since a is not equal to 5

(a*b >= c) // evaluates to true, since (2*3 >= 6) is true

(b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false

((b=2) == a) // evaluates to true


Logical operators
( !, &&, || )
NOT (!)

!(5 == 5) // evaluates to false because the expression at its right (5 == 5)


is true

!(6 <= 4) // evaluates to true because (6 <= 4) would be false

!true // evaluates to false

!false // evaluates to true


short-circuit evaluation
&& OPERATOR (and)
a b a && b
true true true
true false false
false true false
false false false

|| OPERATOR (or)
a b a || b
true true true
true false true
false true true
false false false
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false )
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )
INCREMENT
Prefix increment
In this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is
used.

int a=1,b;
b=++a;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 2 and b is 2

Postfix increment
In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used.

int a=1,b;
b=a++;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 2 and b is 1
DECREMENT
Prefix decrement
In this method, the operator precedes the operand (e.g., --a). The value of operand will be altered before it is
used.

int a=1,b;
b=--a;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 0 and b is 0

Postfix decrement
In this method, the operator follows the operand (e.g., a--). The value operand will be altered after it is used.

int a=1,b;
b=a--;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 0 and b is 1
What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
Output:
int x, y;
x = 5; 7 42
y = ++x * ++x; 7 35
cout << x <<"\t"<< y<<endl;
x = 5;
y = x++ * ++x;
cout << x <<"\t"<< y<<endl;
}
Conditional Operator
Syntax:
exp1 ? exp2 : exp3
Working of the ? Operator:
§ exp1 is evaluated first
• if exp1 is true(nonzero) then
- exp2 is evaluated and its value becomes the value of the expression
• If exp1 is false(zero) then
- exp3 is evaluated and its value becomes the value of the expression
Ex: Ex:
m=2; m=2;
n=3; n=3;
r=(m>n) ? m : n; r=(m<n) ? m : n;
Value of r will be 3 Value of r will be 2
Conditional ternary operator ( ?: )
The conditional operator evaluates an expression, returning one value if that expression
evaluates to true, and a different one if the expression evaluates as false. Its syntax is:

condition ? result1 : result2

7==5 ? 4 : 3 // evaluates to 3, since 7 is not equal to 5.


7==5+2 ? 4 : 3 // evaluates to 4, since 7 is equal to 5+2.
5>3 ? a : b // evaluates to the value of a, since 5 is greater than 3.
a>b ? a : b // evaluates to whichever is greater, a or b.
// Use of conditional operator
#include <iostream>
using namespace std;
int main ()
{
int a,b,c; OUTPUT
a=2;
b=7; 7
c = (a>b) ? a : b;
cout << c << '\n'; }
Bitwise operators ( &, |, ^, ~, <<, >> )
Bitwise operators modify variables considering the bit patterns that represent the values they store.

operator asm equivalent description


& AND Bitwise AND
| OR Bitwise inclusive OR
^ XOR Bitwise exclusive OR
Unary complement (bit
~ NOT
inversion)
<< SHL Shift bits left
>> SHR Shift bits right
Bitwise Operator Examples
8 = 1000 (In Binary)
6 = 0110 (In Binary)
Bitwise & (AND) Bitwise | (OR)
int a=8,b=6,c; int a=8,b=6,c;
c = a & b; c = a | b;
cout<<"Output ="<< c; cout<<"Output ="<< c;
Output = 0 Output = 14
Bitwise << (Shift Left) Bitwise >> (Shift Right)
int a=8,b=6,c; int a=8,b=6,c;
c = a << 1; c = a >> 1;
cout<<"Output ="<< c; cout<<"Output ="<< c;
Output = 16 Output = 4
left shifting is the equivalent of right shifting is the equivalent
multiplying a by a power of two of dividing a by a power of two
Let a = 5, b = 9
1. a&b = 1
2. a|b = 13
3. a^b = 12
& (AND) | (OR)
4. b<<1 = 18
5- 00000101 5- 00000101
9- 00001001 9- 00001001
5. b>>1 = 4
1- 00000001 13-00001101
^ (XOR)
5- 00000101 << Left shift >> Right shift
9- 00001001 9- 00001001 9- 00001001
12-00001100 18-00010010 4-00000100
Bitwise Not (~)

• Tilde symbol (~) is used for bitwise NOT


Consider this program--
#include<iostream>
using namespace std;
int main() {
int a=2;
int c=~a;
cout<<c;
return 0; }
To solve this You will first convert the value of a that is 2 into binary.
2 in binary is 0010
You may consider 8 bits also
00000010
(To keep things simple, I am assuming 4 bits)

Now ~a will be (invert the bits)


1101
You might think answer is the decimal representation of this value, that is 13 !! But its not

Why?? Because if you would have taken 8 bits then answer would have been different !!
And if you would have taken 16 bits or 32 bits then answer would have been different !!

Because int takes 4 bytes (1 byte=8 bits) = 32 bits in total


So what would be the answer??
Bitwise NOT here gives you a negative number actually
See in 1101, Most significant bit is 1, that means a negative number.
So to know the answer, keep the MSB intact and reverse all other bits à
you get 1010
Now Add 1 to this à 1010
+1
1011
1011 is the required answer.
Here Most significant bit 1 means – (minus) and rest 3 bits are giving the value
So answer is -3
Logical NOT (!) -- REVISE
• It gives value in terms of 0 or 1

Lets say
int a=10, b=10, c;
c=(!a==b);
cout<<c;

It will print value 0


If values of a and b would have been different then a==b would have
produced 0 and its NOT = 1.
Assignment operator
• We assign a value to a variable using the basic assignment
operator (=).
• Assignment operator stores a value in memory.
• The syntax is
leftSide = rightSide ;

It is either a literal |
Always it is a
a variable identifier |
variable identifier.
an expression.

Literal: ex. i = 1;
Variable identifier: ex. start = i;
Expression: ex. sum = first + second;
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;
It is an arithmetic
operator.

Ex: Simple assignment


Shorthand operator
x=x+3; operator
x+=3; a = a+1 a += 1
a = a-1 a -= 1
a = a * (m+n) a *= m+n
a = a / (m+n) a /= m+n
a = a % b a %= b
Compound assignment
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

expression equivalent to...


y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
Comma operator ( , )

• Used to separate two or more expressions that are


included where only one expression is expected.
• When the set of expressions has to be evaluated for a
value, only the right-most expression is considered.
For example, the following code:
a = (b=3, b+2);
It would first assign the value 3 to b, and then
assign b+2 to variable a. So, at the end,
variable a would contain the value 5 while
variable b would contain value 3.
New Operators in C++ It allows to access to the global
version of variable
:: Scope Resolution Declares a pointer to a member of
a class
::* Pointer-to-member declarator
->* Pointer-to-member operator To access pointer to class members

.* Pointer-to-member operator To access pointer to data members


of class
new Memory allocation operator
Allocates memory at run time
delete Memory release operator
endl Line feed operator Deallocates memory at run time

setw Field width operator It is a manipulator causes a linefeed


to be inserted
It is a manipulator specifies a field
width for printing value
Scope Resolution Operator(::)
....
....
Declaration of x in inner block hides
{ declaration of same variable declared in an
int x=10; outer block.
....
.... Therefore, in this code both variable x
{ refers to different data.
int x=1; Block-1
.... Block-2
.... § In C language, value of x declared in Block-1 is
} not accessible in Block-2.
.... § In C++, using scope resolution operator (::),
}
value of x declared in Block-1 can be accessed
in Block-2.
#include <iostream> Scope resolution example
using namespace std;
int m=10; Global declaration of variable m
int main()
{
int m=20; variable m declared , local to main
{
int k=m;
int m=3;
cout<<"we are in inner block\n";
cout<<"k="<<k<<endl; variable m
cout<<"m="<<m<<endl; declared again local to inner block
cout<<"::m="<<::m<<endl;
Output:
}
we are in inner block
cout<<"we are in outer block\n";
cout<<"m="<<m<<endl; k=20
cout<<"::m="<<::m<<endl; m=3
return 0; ::m=10
} we are in outer block
m=20
::m=10
Another classification of Operators
The data items on which operators act upon are called operands.

1) Unary Operators: Those operators that require only single operand to act upon are known as
unary operators.
For Example: unary minus (-), increment (++) and decrement (--) operators

2) Binary Operators: Those operators that require two operands to act upon are called binary
operators.
Binary operators are classified into :
Arithmetic operators, Relational Operators, Logical Operators, Assignment Operators,
Bitwise Operators

3) Ternary Operators: These operators requires three operands to act upon. For Example:-
Conditional operator(?:).
Unary Operators
SrNo Operators Symbols
1 Unary plus +
2 Unary minus -
3 Increment operator ++
4 Decrement operato --
5 Address of Operator &
6 Size of Operator sizeof()
7 Dereferencing *
Operator
8 Logical NOT !
9 Bitwise NOT/ ~
Bitwise Negation/
One's Compliment
Unary minus
The minus operator changes the sign of its argument. A
positive number becomes negative, and a negative number
becomes positive.

int a = 10;
int b = -a; // b = -10
NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
If x is true, then !x is false
If x is false, then !x is true

Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a
variable.

& gives an address on variable n


int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr.
sizeof(): This operator returns the size of its operand, in bytes. The sizeof operator
always precedes its operand. The operand is an expression, or it may be a cast.

#include <iostream>
using namespace std;

int main()
{
float n = 0;
cout << "size of n: " << sizeof(n);
return 0;
}
OUTPUT: size of n: 4
Precedence Operator Meaning
1 – (unary) Returns the negative of its argument

2 ++ (unary) Increment
2 — (unary) Decrement
3 * (binary) Multiplication
3 / (binary) Division
3 % (binary) Modulo
4 + (binary) Addition
4 – (binary) Subtraction
5 =, *=,%=,+=,-= Assignment types
Precedence Example

x = 5 + 7 % 2; gives x=6
x = 5 + (7 % 2); gives x = 6
x = (5 + 7) % 2; gives x = 0

You might also like