CS201- Introduction to Programming- Lecture 21
-
Upload
bilal-ahmed -
Category
Education
-
view
24 -
download
1
description
Transcript of CS201- Introduction to Programming- Lecture 21
Introduction to Introduction to ProgrammingProgramming
Lecture 21Lecture 21
Today’s LectureToday’s Lecture
Bit manipulation Bit manipulation Bit operatorsBit operators
Logical OperatorsLogical Operators
ANDAND&&&&
OROR ||||
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
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
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
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 ;
}}
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
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
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
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
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
NOT OperatorNOT Operator
Truth table for NOT operatorTruth table for NOT operator
AA ~A~A
00 11
11 00
NOT OperatorNOT Operator
x = 8x = 8
~ ( 1000 ) = 0111 ~ ( 1000 ) = 0111
= 7= 7
Bit FlagsBit Flags
Read Write And ExecuteRead Write And Execute
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
RaidRaidRedundant Array of Inexpensive DevicesRedundant Array of Inexpensive Devices
Hot PlugHot Plug
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 ;
Unsigned Unsigned integerinteger
unsigned int i , j , k ;unsigned int i , j , k ;
Left ShiftLeft ShiftA number 1A number 1
Shift leftShift left
1
10
Right ShiftRight ShiftA number 10A number 10
Right shiftRight shift
10
01
Left & Right Shift Left & Right Shift Operator Operator
<< shift left<< shift left >> shift right>> shift right
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
1100
1100000011110011
Left shift
Right shift
11 11 00 00 00 11
00