cs201- introduction to programming- lecture 21
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 21 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 21Lecture 21
![Page 2: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/2.jpg)
Today’s LectureToday’s Lecture
Bit manipulation Bit manipulation Bit operatorsBit operators
![Page 3: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/3.jpg)
Logical OperatorsLogical Operators
ANDAND&&&&
OROR ||||
![Page 4: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/4.jpg)
Bit manipulation operatorsBit manipulation operators&& Bitwise AND OperatorBitwise AND Operator
| | Bitwise OR OperatorBitwise OR Operator
^̂ Bitwise Exclusive OR Bitwise Exclusive OR OperatorOperator
~~ NOT OperatorNOT Operator
<<<< Left Shift OperatorLeft Shift Operator
>> >> Right Shift OperatorRight Shift Operator
![Page 5: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/5.jpg)
Bitwise AND OperatorBitwise AND OperatorTruth table for AND operationTruth table for AND operation
Bit1Bit1 Bit2Bit2 Bit1Bit1 & & BBit2it2
11 11 11
11 00 00
00 11 00
00 00 00
![Page 6: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/6.jpg)
Bitwise AND Operator ExampleBitwise AND Operator Example
………… ………… 2233 2 222 2211 2200
12 = 1 1 0 0
8 = 1 0 0 0 &
_______________
1 0 0 0
Hence x = 12 & 8 = 8
![Page 7: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/7.jpg)
ExampleExample#include <iostream.h>#include <iostream.h>
main ( )main ( )
{{
int number = 12 ;int number = 12 ;
if ( number & 0x8 )if ( number & 0x8 )
cout << "Bit number four is set" << endl ;cout << "Bit number four is set" << endl ;
elseelse
cout << "Bit number four is not set" << cout << "Bit number four is not set" << endl ;endl ;
}}
![Page 8: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/8.jpg)
Bitwise OR OperatorBitwise OR OperatorTruth table for OR operationTruth table for OR operation
A A BB A|BA|B
11 11 11
11 00 11
00 11 11
00 00 00
![Page 9: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/9.jpg)
Bitwise OR OperatorBitwise OR Operator Example 1 Example 1
X = 12 | 8X = 12 | 8
1 1 0 0
1 0 0 0|
_____________
1 1 0 0
Hence the result x = 12
![Page 10: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/10.jpg)
Bitwise OR OperatorBitwise OR Operator Example 2 Example 2
x = 8 | 1x = 8 | 1
1 0 0 0
0 0 0 1|
_____________
1 0 0 1
Hence the result x = 9
![Page 11: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/11.jpg)
Bitwise Exclusive OR Bitwise Exclusive OR OperatorOperator
Truth table for Exclusive OR operationTruth table for Exclusive OR operation
A A BB A^BA^B
11 11 00
11 00 11
00 11 11
00 00 00
![Page 12: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/12.jpg)
Example: Exclusive OR Example: Exclusive OR OperatorOperator
X = 8 ^ 1X = 8 ^ 11 0 0 0
0 0 0 1^_____________1 0 0 1Result x = 9
^0 0 0 1_____________1 0 0 0Result x = 8
X = 9 ^ 1
![Page 13: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/13.jpg)
NOT OperatorNOT Operator
Truth table for NOT operatorTruth table for NOT operator
AA ~A~A
00 11
11 00
![Page 14: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/14.jpg)
NOT OperatorNOT Operator
x = 8x = 8
~ ( 1000 ) = 0111 ~ ( 1000 ) = 0111
= 7= 7
![Page 15: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/15.jpg)
Bit FlagsBit Flags
![Page 16: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/16.jpg)
Read Write And ExecuteRead Write And Execute
![Page 17: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/17.jpg)
Exclusive OR Exclusive OR OperatorOperatorExampleExample
unsigned int a , b , c ;unsigned int a , b , c ;
a = 112 ; a = 112 ;
b = 32 ; b = 32 ;
c = a ^ b ; c = a ^ b ;
c = ( a ^ b ) ^ b ; c = ( a ^ b ) ^ b ; the result is athe result is a
c = ( a ^ b ) ^ a ; c = ( a ^ b ) ^ a ; the result is bthe result is b
![Page 18: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/18.jpg)
RaidRaidRedundant Array of Inexpensive DevicesRedundant Array of Inexpensive Devices
![Page 19: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/19.jpg)
Hot PlugHot Plug
![Page 20: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/20.jpg)
ExampleExample Swapping two integers Swapping two integers
without a temporary without a temporary storagestorage
unsigned int a = 12 unsigned int a = 12 ;;
unsigned int b = 8 ;unsigned int b = 8 ;
a = a ^ b ;a = a ^ b ;
b = b ^ a ;b = b ^ a ;
a = a ^ b ;a = a ^ b ;
![Page 21: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/21.jpg)
Unsigned Unsigned integerinteger
unsigned int i , j , k ;unsigned int i , j , k ;
![Page 22: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/22.jpg)
Left ShiftLeft ShiftA number 1A number 1
Shift leftShift left
1
10
![Page 23: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/23.jpg)
Right ShiftRight ShiftA number 10A number 10
Right shiftRight shift
10
01
![Page 24: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/24.jpg)
Left & Right Shift Left & Right Shift Operator Operator
<< shift left<< shift left >> shift right>> shift right
![Page 25: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/25.jpg)
Left & Right Shift Left & Right Shift OperatorOperator
Unsigned int i = 4 ;Unsigned int i = 4 ;
i << 1 ; i << 1 ; shift leftshift left
i >> 1 ; i >> 1 ; shift rightshift right
![Page 26: CS201- Introduction to Programming- Lecture 21](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e76cb47959834a8b467c/html5/thumbnails/26.jpg)
1100
1100000011110011
Left shift
Right shift
11 11 00 00 00 11
00