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

Lecture 5

This document discusses type casting and operators in C++. It explains type conversion that occurs during assignments when different data types are mixed. It provides examples of implicit type conversions for assignments between integer, character, and float types. It also describes various operators in C++ like arithmetic, relational, logical, and bitwise operators. It explains operator precedence and provides examples of expressions using these operators.

Uploaded by

Bilal Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 5

This document discusses type casting and operators in C++. It explains type conversion that occurs during assignments when different data types are mixed. It provides examples of implicit type conversions for assignments between integer, character, and float types. It also describes various operators in C++ like arithmetic, relational, logical, and bitwise operators. It explains operator precedence and provides examples of expressions using these operators.

Uploaded by

Bilal Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

Lecture 5

Type Casting & Operators


Type Conversion/Casting
 C++ is not a strongly typed language.
 Can assign one data type to other data type.
 Hence, it is possible to mix different types in one
expression, and in such situations, type conversion will
occur.

 In an assignment statement, type conversion rule is:

 The value of the right side of the assignment is


converted to the type of the left side.
 Consider the following example:
Example
int x;
char ch;
float f;

void function1()
{
ch = x;
x = f;
f = ch;
f = x;
}
ch = x
 Here ch is a character while x is an integer.

 A character takes 1 byte while an integer takes 4 bytes.

 Hence, the most significant three bytes of the integer are


ignored, and the least significant byte is assigned to the
character ch.
The output would be “A”. The reason
 int x = 321; behind this is that 321 in binary notation is
char ch; 00000000 00000000 00000001 01000001
The three most significant bytes, when
ch = x; ignored, leaves us with 01000001 which is
cout << ch; 65 in decimal and “A” in character. 65 is
the ASCII code for A.
x=f
 Here, x is an integer while f is a floating point number.

 An integer cannot have a decimal part, hence, when a


floating point number is assigned to an integer, its
decimal part is ignored.

int x; Here, the output would be


float f = 22.924567888; 22
and
x = f; 22.9246
cout << x << endl; Real numbers are rounded off
to 6 digits. We will later see if
cout << f << endl;
we want to have more precision.
f = ch
 In this case, the character value is converted into a float
value and saved into the variable f.

 char ch = 'A';
float f; Here, the output would be
A
f = ch; and
cout << ch << endl; 65
The character byte is stored in the
cout << f << endl; least significant byte of the float
and the rest of the bytes are 0.
f=x
 Here, the four bytes of the integer would become the
four bytes of the floating point number.

 int x = 24;
float f;
Here, the output would be
f = x; 24 and 24
cout << f << endl; The integer bytes are stored in the
same bytes of the float. That is
cout << x << endl; why they have the same value.
Operators
 In C++, there are many kinds of operators. These are:

 Assignment Operator
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
Assignment Operator
 General Form
 variable = expression;

 Example:
 a = a + 1;
 a += 1;
 a = b + c;
 a = (b + (d – (x * y)));
Multiple Assignments
 int x, y, z;
 x = y = z = 0;

 OR

 int x = 0, y = 0, z = 0;
Left shifts 2 bits
Arithmetic Operators and Operator Precedence
 C++ arithmetic operators:
 + addition
 - subtraction
 * multiplication
 / division
 % modulus operator
 -- (Decrement)
 ++ (Increment)
 +, -, *, and / can be used with integral and floating-point data
types
 Mathematical Operators can be unary or binary
 Unary operators perform an action with a single operand, i.e., A=1; B =
a++. Binary operators perform actions with two operands, i.e., a + b.

12
Expressions
 If all operands are integers
 Expression is called an integral expression
 Yields an integral result

 Example: 2 + 3 * 5

 If all operands are floating-point


 Expression is called a floating-point expression
 Yields a floating-point result

 Example: 12.8 * 17.5 - 34.50

13
Mixed Expressions
 Mixed expression:
 Has operands of different data types
 Contains integers and floating-point
 Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
C ++ o p e ra tio n Arith m e tic Alg e b ra ic C ++ e xp re ssio n
o p e ra to r e xp re ssio n
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s

14
Mixed Expressions (continued)
 Evaluation rules:
 If operator has same types of operands
 Evaluated according to the type of the operands

 If operator has both types of operands


 Integer is changed to floating-point

 Operator is evaluated

 Result is floating-point

 Entire expression is evaluated according to precedence


rules

15
Rules of Operator Precedence
 All operations inside of () are evaluated first
 *, /, and % are at the same level of precedence and are evaluated next
 + and – have the same level of precedence and are evaluated last
 When operators are on the same level
 Performed from left to right (associativity)
 3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6

16
Sample Algebraic and C++ Expressions
 For the following expression:

 The parentheses are required because division has higher


precedence than addition.
 If the parentheses are erroneously omitted, we obtain a + b +
c + d + e / 5.

Example: ax2+bx+c
 Suppose variables a , b , c and x in the preceding
second-degree polynomial are initialized as follows:
 a = 2 , b = 3 , c = 7 and x = 5
 Solve it for answer 72
Pre and Post fix Operations
 The ++ operator is called as increment operator.
 The – operator is called as decrement operator.
 Consider this example:

 int x = 10;
 int y = x++; Output would be:
 cout << y << endl; 10
 cout << x; 11

 int x = 10;
Output would be:
 int y = ++x;
11
 cout << y << endl;
11
 cout << x;
Pre and Post fix Operations

 l-value (locator value) represents an object that occupies


some identifiable location in memory.
Addition/Subtraction/Multiplication/Division

 int x = 30, y = 20, z;


z = x + y;
cout << "Result of addition: " << z << endl;
z = x - y;
cout << "Result of subtraction: " << z << endl;
z = x * y;
cout << "Result of multiplication: " << z << endl;
z = x / y;
cout << "Result of division: " << z << endl;
Output
Result of Division ?
 Why is the result 1 and not 1.5?

 int x = 30, y = 20;


int z;
z = x / y;
cout << "Result of division: " << z << endl;
Result of Division ?
 Why is the result 1 and not 1.5?

 int x = 30, y = 20;


double z;
z = x / y;
cout << "Result of division: " << z << endl;

 What about now?


1
Division
 For proper division, at least one of the operands would
be of type double.

 int x = 30, y = 20;


double z;
z = double(x) / y;
cout << "Result of division: " << z << endl;
Division
The Modulus Operator
 % is called the modulus operator.

 Modulus operator calculates the remainder of two numbers.

 10 % 2 = 0
 11 % 2 = 1
 13 % 3 = 1
 14 % 4 = 2
 19 % 5 = 4
 9% 11= 9 // if number is smaller, it becomes the remainder.
Sample Code
#include <iostream>
using namespace std;

int main()
{
cout << "Result of Modulus of 10 % 2 = " << (10 % 2) << endl;
cout << "Result of Modulus of 11 % 2 = " << (11 % 2) << endl;
cout << "Result of Modulus of 13 % 3 = " << (13 % 3) << endl;
cout << "Result of Modulus of 14 % 4 = " << (14 % 4) << endl;
cout << "Result of Modulus of 19 % 5 = " << (19 % 5) << endl;
cout << "Result of Modulus of 9 % 11 = " << (9 % 11) << endl;
system("pause");
return 0;
}
Output
Relational and Logical Operators
 A relational operator is used to check the relationship
between two operands, e.g., a is greater than b

 Logical operators are used to check whether an


expression is true or false. If the expression is true, it
returns 1 whereas if the expression is false, it returns 0..

 For both relational and logical operators, the main


concept is of true and false.

 In C++, true is any non-zero value. Consequently, false


is 0. Expressions that use logical or relational operators
return 1 for true and 0 for false.
Relational and Logical Operators
 C++ defines another data type which is called as bool.
 bool is basically “boolean” which can take any of the
following two values.
 true (1)
 false (0)
Logical Operators
 &&
 performs AND of two variables.

 ||
 performs OR of two variables.

 !
 performs NOT of a variable.
Logical Operators
 &&
 true AND true = true
 true AND false = false
 false AND true = false
 false AND false = false
!
 || ! true = false
 true OR true = true ! false = true
 true OR false = true
 false OR true = true
 false OR false = false
Relational Operators
 > (Greater than)
 < (Less than)
 >= (Greater than or equal to)
 <= (Less than or equal to)
 == (Equals to)
 != (Not equals to)

 10 > 15 (returns 0)
 10 <= 10 (returns 1)
 10 != 22 (returns 1)
 10 == 21 (returns 0)
Relational and Logical Operators
 How about this segment of code ?

 10 > 5 && (10 < 9) || ! (2 < 1) || 3 <= 0


 1 && 0 || 1 || 0
 0 || 1 || 0
 1 || 0
 1

 10 > 5 && (((10 < 9) || ! (2 < 1)) || 3 <= 0)


 10 > 5 && ((0 || 1) || 3 <= 0)
 10 > 5 && (1 || 0)
 1 && 1
 1
Relational and Logical Operators
 Precedence
 Highest !
 >, >=, <, <=
 ==
 &&
 Lowest ||
Relational and Logical Operators
 !0 && 0 || 0

 Output ?
Task:
Write the code
 !(0 && 0) || 0
for XOR code
 Output ?
0 0 0
0 1 1
 int x = 10; 1 0 1
 cout << (x > 20); 1 1 0
 Output ?
Relational Operators: ==, !=, >, <, >=, <=
 #include <iostream>
 using namespace std;
 int main() {
 int a, b;
 a = 3;
 b = 5;
 bool result;

 result = (a == b); // false


 cout << "3 == 5 is " << result << endl;

 result = (a != b); // true


 cout << "3 != 5 is " << result << endl;

Output :
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
Logical Operators: &&, ||, !
 #include <iostream>  result = (3 != 5) || (3 < 5); // true
 using namespace std;
 cout << "(3 != 5) || (3 < 5) is " << result << endl;
 int main() {
 bool result;  result = (3 != 5) || (3 > 5); // true
 cout << "(3 != 5) || (3 > 5) is " << result << endl;
 result = (3 != 5) && (3 < 5); // true
 cout << "(3 != 5) && (3 < 5) is " << result << endl;
 result = (3 == 5) || (3 > 5); // false
 result = (3 == 5) && (3 < 5); // false  cout << "(3 == 5) || (3 > 5) is " << result << endl;
 cout << "(3 == 5) && (3 < 5) is " << result << endl;
 result = !(5 == 2); // true
 result = (3 == 5) && (3 > 5); // false
 cout << "(3 == 5) && (3 > 5) is " << result << endl;
 cout << "!(5 == 2) is " << result << endl;

 result = !(5 == 5); // false


 cout << "!(5 == 5) is " << result << endl;

 return 0;
 }
Output:
(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0
Bitwise Operators
 Bitwise operators operate at the bit levels
 Bitwise operation refers to testing, setting, shifting the
actual bits in a byte, which correspond to int or char
data types only.

 Bitwise operations can not be used for float, double,


void, etc.
Bitwise Operators
 & bitwise AND
 | bitwise OR
 ^ bitwise XOR
 ~ bitwise NOT
 >> Shift Right
 << Shift Left

 Let us see their functionalities one by one.


Bitwise AND
 It is typically used to clear a bit.

 E.g.

 char a = 11;
0 0 0 0 1 0 1 1

 a = a & 8;
0 0 0 0 1 0 1 1
0 0 0 0 1 0 0 0 &

0 0 0 0 1 0 0 0
Bitwise AND
 Also used to get some selected bits value.

 E.g. consider that bit 2 (starting from 1) is for BOLD, bit 4


is for ITALIC, and bit 7 is for UNDERLINE.

 If we want to get only these values from a byte, we will


define a mask. The mask would be
 0 1 0 0 1 0 1 0
 i.e. 74.

 Now consider this example:


bitwise AND
 char a = 34;
 char b = 74;
 a = a & b;

 0 0 1 0 0 0 1 0
 0 1 0 0 1 0 1 0 &

 0 0 0 0 0 0 1 0 =2

 If value after this operation in a is 2, then only Bold bit is set. If value
is 8, then only Italic bit is set. If value is 64, then only underline bit is
set. If value is 10, then both bold and italic bits are set. If value is 66,
then both underline and bold bits are set, and so on.
bitwise OR
 It is used to set a bit. E.g. you want to set the bold and
underline bits, but not set the italic bit. Here the mask would
be

 0 1 0 0 0 0 1 0 = 66

 char a = 22;

 0 0 0 1 0 1 1 0
 0 1 0 0 0 0 1 0 |

 0 1 0 1 0 1 1 0 = 86
bitwise XOR
 Will set bits if they are different.

 0 0 0 1 0 1 1 0
 0 1 0 0 0 0 1 0 ^

 0 1 0 1 0 1 0 0 =
84

 a = a ^ b;
bitwise NOT
 Reverses the bit pattern (one’s complement)

 char a = 131;

 1 0 0 0 0 0 1 1

 a = ~a;

 0 1 1 1 1 1 0 0 = 124
Bitwise Operators: &, |, ^, ~, >>, <<
12 = 00001100 (In Binary) #include <iostream>
using namespace std;
25 = 00011001 (In Binary)
int main() {
//Bitwise AND Operation of 12 and 25
// declare variables
00001100 int a = 12, b = 25;
& 00011001
_________ cout << "a = " << a << endl;
00001000 = 8 (In decimal) cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
}

Output:

a = 12
b = 25
a&b=8
Bitwise Operators: &, |, ^, ~, >>, <<

12 = 00001100 (In Binary) #include <iostream>


25 = 00011001 (In Binary)
int main() {
Bitwise OR Operation of 12 and 25 int a = 12, b = 25;
00001100
| 00011001 cout << "a = " << a << endl;
_________ cout << "b = " << b << endl;
00011101 = 29 (In decimal) cout << "a | b = " << (a | b) << endl;

return 0;
}

Output:

a = 12
b = 25
a | b = 29
Bitwise Operators: &, |, ^, ~, >>, <<

12 = 00001100 (In Binary) #include <iostream>


25 = 00011001 (In Binary)
int main() {
Bitwise XOR Operation of 12 and 25 int a = 12, b = 25;
00001100
^ 00011001 cout << "a = " << a << endl;
_________ cout << "b = " << b << endl;
00010101 = 21 (In decimal) cout << "a ^ b = " << (a ^ b) << endl;

return 0;
}

Output:

a = 12
b = 25
a ^ b = 21
Bitwise Operators: &, |, ^, ~, >>, <<
35 = 00100011 (In Binary) #include <iostream>
// Using bitwise complement operator int main() {
~ 00100011
int num1 = 35;
__________
11011100 int num2 = -150;
cout << "~(" << num1 << ") = " <<
•It is important to note that the bitwise (~num1) << endl;
complement of any integer N is equal cout << "~(" << num2 << ") = " <<
to -(N + 1).  (~num2) << endl;
•The bitwise complement of 35 should return 0;
be -(35 + 1) = -36 }
•36 = 00100100 (In Binary)
•1's Complement = 11011011
•2's Complement: Output:
•11011011
•+ 1 The bitwise complement of 35 = - (35 + 1) = -36
_________ The bitwise complement of -150 = - (-150 + 1) =
•11011100 - (-149) = 149
Bitwise Operators: &, |, ^, ~, >>, <<

One bit Right Shift One bit Left Shift

#include <iostream>
// Shift Left Operation
cout << "\nShift Left:" << endl;
int main() {
// Using for loop for shifting num left from 0 bit to
// declaring two integer variables
3 bits
int num = 212, i;
for (i = 0; i < 4; i++) {
cout << "212 << " << i << " = " << (212 << i)
// Shift Right Operation
<< endl;
cout << "Shift Right:" << endl;
}
// Using for loop for shifting num right from 0 bit to 3
return 0;
bits
}
for (i = 0; i < 4; i++) {
cout << "212 >> " << i << " = " << (212 >> i) <<
endl;
}
Bitwise Operators: &, |, ^, ~, >>, <<
From the output of the program
above, we can infer that, for any
Output
number N, the results of the shift
right operator are:
Shift Right:
212 >> 0 = 212
N >> 0 = N
212 >> 1 = 106
N >> 1 = (N >> 0) / 2
212 >> 2 = 53
N >> 2 = (N >> 1) / 2
212 >> 3 = 26
N >> 3 = (N >> 2) / 2
and so on.
Shift Left:
212 << 0 = 212
Similarly, the results of the shift
212 << 1 = 424
left operator are:
212 << 2 = 848
212 << 3 = 1696
N << 0 = N
N << 1 = (N << 0) * 2
N << 2 = (N << 1) * 2
N << 3 = (N << 2) * 2
Example
#include<iostream>
#include<math.h>
using namespace std;
void main()
{
float f=34.34343434;
cout<<"Value is = "<<f<<endl;
cout<<"f raise to the power 2="<<pow(f,2)<<endl;
cout<<"Ceil = "<<ceil(f)<<endl;
cout<<"Floor = "<<floor(f)<<endl;
cout<<"Square root is = "<<sqrt(f)<<endl;
}

You might also like