Itp Lab 5 - 20tlii

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________

Name: ____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: ______________ Date: ___________

To Familiarize with Logical Operators in C++

Lab Performance Subject Data Analysis And Ability To Conduct Calculation And
Presentation Observation/ Results Score
Indicator Knowledge Interpretation Experiment Coding

Objective No:

PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to learn:
logical, bitwise, assignment and other operators one by one.

Discussion
Once introduced to variables and constants, we can begin to operate with them by using
operators. What follows is a complete list of logical operators. At this point, it is likely not
necessary to know all of them, but they are all listed here to serve as reference. C++ is having
very rich set of logical operators.

A logical operator is a symbol that tells the compiler to perform specific logical operations. C++
is rich in built-in logical operators.

Logical Operators
Following logical operators supported by C++ language. Assume variable A holds 5 and variable
B holds 20, then:

Example:

#include <iostream>
using namespace std;
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
cout << "Line 1 - Condition is true"<< endl ;
}
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
if ( a || b ) {
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if ( a && b ) {
cout << "Line 3 - Condition is true"<< endl ;
} else {
cout << "Line 4 - Condition is not true"<< endl ;
}
if ( !(a && b) ) {
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}

Operator Description Example

&& Called Logical AND operator. If (A && B) is false.


both the operands are non-zero, then
condition becomes true.

|| Called Logical OR Operator. If any (A || B) is true.


of the two operands is non-zero,
then condition becomes true.

! Called Logical NOT Operator. Use !(A && B) is true.


to reverses the logical state 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:
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________

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

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

The Bitwise operators supported by C++ language are listed in the following table. Assume
variable A holds 60 and variable B holds 13, then:

Example:

#include <iostream>
using namespace std;
main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a & b; // 12 = 0000 1100
cout << "Line 1 - Value of c is : " << c << endl ;
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
c = a | b; // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - Value of c is: " << c << endl ;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - Value of c is: " << c << endl ;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - Value of c is: " << c << endl ;
return 0; }}

Operator Description Example

& Binary AND Operator copies a (A & B) will give 12 which is


bit to the result if it exists in both 0000 1100
operands.

| Binary OR Operator copies a bit (A | B) will give 61 which is


if it exists in either operand. 0011 1101

^ Binary XOR Operator copies the (A ^ B) will give 49 which is


bit if it is set in one operand but 0011 0001
not both.

~ Binary Ones Complement (~A ) will give -61 which is


Operator is unary and has the 1100 0011 in 2's complement
effect of 'flipping' bits. form due to a signed binary
number.

<< Binary Left Shift Operator. The A << 2 will give 240 which is
left operands value is moved left 1111 0000
by the number of bits specified
by the right operand.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
>> Binary Right Shift Operator. The A >> 2 will give 15 which is
left operands value is moved 0000 1111
right by the number of bits
specified by the right operand.

Assignment Operators
There are following assignment operators supported by C++ language:

Example:

#include <iostream>
using namespace std;
main() {
int a = 21;
int c ;
c = a;
cout<<”Observe the operation of some more assignment operators”<<endl;
c <<= 2;
cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ;
c >>= 2;
cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ;
c &= 2;
cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;
c ^= 2;
cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;
c |= 2;
cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;
return 0;
}

Operator Description Example

<<= Left shift AND assignment C <<= 2 is same as C = C << 2


operator
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
>>= Right shift AND assignment C >>= 2 is same as C = C >> 2
operator

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


operator

^= bitwise exclusive OR and C ^= 2 is same as C = C ^ 2


assignment operator

|= bitwise inclusive OR and C |= 2 is same as C = C | 2


assignment operator

Misc Operators
There are few other operators supported by C++ Language.

Operator Description

sizeof sizeof operator returns the size of a variable. For


example, sizeof(a), where a is integer, will return 4.

Condition ? X : Y Conditional operator. If Condition is true ? then it


returns value X : otherwise value Y

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

. (dot) and -> (arrow) Member operators are used to reference individual
members of classes, structures, and unions.

Cast Casting operators convert one data type to another. For


example, int(2.2000) would return 2.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

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

* Pointer operator * is pointer to a variable. For example


*var; will pointer to a variable var.

Operators Precedence in C++


Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator:

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has 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.

Example:

#include <iostream>
using namespace std;
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
cout << "Value of (a + b) * c / d is :" << e << endl ;
e = ((a + b) * c) / d; // (30 * 15 ) / 5
cout << "Value of ((a + b) * c) / d is :" << e << endl ;
e = (a + b) * (c / d); // (30) * (15/5)
cout << "Value of (a + b) * (c / d) is :" << e << endl ;
e = a + (b * c) / d; // 20 + (150/5)
cout << "Value of a + (b * c) / d is :" << e << endl ;
return 0; }
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

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


DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 5
ST

________________________________________________________________________
Comma , Left to right

EXERCISE

1. Following table gives logical expressions along with their meanings.

Logical Expression Meaning

x<2 Is x less than 2?

b*b - 4*a*c >= 0 Is b2 - 4ac greater than or equal to 0?

answer == 'Y' Is answer equal to 'Y'?

flag && done Is flag true and done true?

y || z Is y or z true?

!EOF Is EOF false?


In the above table, if x = 5, a = 1, b = 2, c = 1, answer = 'N', flag = true, done =
false, y = 1, z = 0, and EOF = false then evaluate the output of the given
expressions.

2. Convert the English expression "answer is neither 'Y' nor 'y'" to a C++ expression.
3. Which C++ logical expression correctly determines whether x and y are greater than z?
a. (x && y > z )
b. (x > z ) && (y > z )
c. ( y > z ) && ( x > z )
d. all of the above
e. both b and c.
RUN ALL THE CODES GIVEN IN EXAMPLES AND OBSERVE THE WORKING OF OPERATORS.

LAB MUST BE SUBMITTED ON OR BEFORE THE NEXT CLASS.

**************** THE END ****************

You might also like