Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views19 pages

FOP Lab 4 NEW

The document covers the fundamentals of programming, focusing on the binary number system, logical operators, and bitwise operators in C++. It explains concepts such as AND, OR, NOT gates, and provides examples of logical and bitwise operations. Additionally, it includes examples of relational operators and their outputs.

Uploaded by

awaisengineer20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

FOP Lab 4 NEW

The document covers the fundamentals of programming, focusing on the binary number system, logical operators, and bitwise operators in C++. It explains concepts such as AND, OR, NOT gates, and provides examples of logical and bitwise operations. Additionally, it includes examples of relational operators and their outputs.

Uploaded by

awaisengineer20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SPRING 2024 – DE 45 MTS

CS – 114
Fundamentals of Programming
(LAB - 4)
Instructors:
Dr. Ayesha Zeb
Dr. Tahir Habib Nawaz
LE Hamza Sohail

Department of Mechatronics Engineering, National University of


Sciences and Technology, College of E&ME
Binary Number System
• 0 and 1.
• Either false or true
• 0 = false
• 1 = true
Conversion

By adding the numbers as shown in


figure.
128 + 64 + 8 + 2 = 202
So, 202 in binary is 11001010

https://www.britannica.com/technology/byte
Logical Operators
• Logical AND Operator &&
• Logical OR Operator ||
• Logical NOT Operator !
AND GATE
• Output will be 1 only if both inputs are 1.

A B A &&B
0 0 0
0 1 0
1 0 0
1 1 1
OR GATE
• Output will be 1 only if any one input is 1.

A B A ||B
0 0 0
0 1 1
1 0 1
1 1 1
NOT GATE
• Output will be 1 if input is 0 and vice versa.

A A’
0 1
1 0
Example of Logical Operator && in C++
• int a,b,c;
• cin>>a>>b>>c; //input a,b,c
• cout<< (a>b && a>c); // AND Operator

// AND means both should be true than output is 1.


// in the above case if a is greater than both b and c than output will be
1 else 0
Example of Logical Operator || in C++
• int a,b,c;
• cin>>a>>b>>c; //input a,b,c
• cout<< (a>b || a>c); // OR Operator

// OR means anyone should be true than output is 1.


// in the above case if a is greater than b OR a is greater than c than
output will be 1 else 0
Example of Logical Operator ! in C++
• int a,b,c;
• cin>>a>>b; //input a,b
• cout<< !(a>b); // NOT Operator

// NOT means input should be false than output is 1.


// in the above case if a is greater than b than output will be 0 else 1
Bitwise Logical Operators &, |, ~, ^, <<, >>
1.The & (bitwise AND) in C or C++ takes two numbers as operands and does
AND on every bit of two numbers. The result of AND is 1 only if both bits are
1.
2.The | (bitwise OR) in C or C++ takes two numbers as operands and does OR
on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
3.The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does
XOR on every bit of two numbers. The result of XOR is 1 if the two bits are
different.
4.The << (left shift) in C or C++ takes two numbers, left shifts the bits of the
first operand, the second operand decides the number of places to shift.
5.The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the
first operand, the second operand decides the number of places to shift.
6.The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
Bitwise & Operator
• Bitwise AND ‘&’ Operator return 1 if both the input bits are 1.
• For example; if we do (12 & 25) the output would be 8.
• 12 = 00001100
• 25 = 00011001
12 & 25 = 00001000
00001000 is equal to 8.
Bitwise | Operator
• Bitwise OR ‘|’ Operator return 1 if any one of the input bit is 1.
• For example; if we do (12 | 25) the output would be 29.
• 12 = 00001100
• 25 = 00011001
12 | 25 = 00011101
00011101 is equal to 29.
Bitwise ~ Operator
• Bitwise NOT ‘~’ Operator return 1 if input bit is 0 and vice versa.
• For example; if we do (~21) the output would be 8.
• 35 = 00100011
• ~35 = 11011100
• Which is equal to 220 but compiler gives -36.
• 220 and -36 are equivalent.
Bitwise ‘XOR’ ^ Operator
• Bitwise XOR ‘^’ Operator return 1 if odd number of inputs are 1 and
vice versa.
• For example; if we do (a^b) the output would be 8.
• 12 = 00001100
• 25 = 00011001
• 12^25 = 00010101
• Which is equal to 21
Bitwise ‘XOR’ ^ Operator
• Example:
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a ^ b = " << (a ^ b) << endl;
Bitwise “<<“ and “>>” Operator
Left and right shift
• Example:
Bitwise Left and Right shift Operators
Example
• Example:
Left shift
int a = 12;
cout << (a<<1) << endl;
// answer is 24

Right Shift
cout<< (a>>1) <<endl;

// answer is 6
Example for Relational Operator
• cout<< (12==12);
• cout<< (12!=12);
• cout<< (12==1);
• cout<< (12>1);
• cout<< (12<=12);
• What is the output of the above code:
• (a) 01100
• (b) 10011
• (c) 10101

You might also like