Lecture 5
Lecture 5
void function1()
{
ch = x;
x = f;
f = ch;
f = x;
}
ch = x
Here ch is a character while x is an integer.
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
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
Operator is evaluated
Result is floating-point
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:
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
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
||
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 ?
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;
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;
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.
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.
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: &, |, ^, ~, >>, <<
return 0;
}
Output:
a = 12
b = 25
a | b = 29
Bitwise Operators: &, |, ^, ~, >>, <<
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: &, |, ^, ~, >>, <<
#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;
}