26
Introduction to Introduction to Programming Programming Lecture 21 Lecture 21

CS201- Introduction to Programming- Lecture 21

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 21 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 21

Introduction to Introduction to ProgrammingProgramming

Lecture 21Lecture 21

Page 2: CS201- Introduction to Programming- Lecture 21

Today’s LectureToday’s Lecture

Bit manipulation Bit manipulation Bit operatorsBit operators

Page 3: CS201- Introduction to Programming- Lecture 21

Logical OperatorsLogical Operators

ANDAND&&&&

OROR ||||

Page 4: CS201- Introduction to Programming- Lecture 21

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

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

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

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

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

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

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

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

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

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

NOT OperatorNOT Operator

x = 8x = 8

~ ( 1000 ) = 0111 ~ ( 1000 ) = 0111

= 7= 7

Page 15: CS201- Introduction to Programming- Lecture 21

Bit FlagsBit Flags

Page 16: CS201- Introduction to Programming- Lecture 21

Read Write And ExecuteRead Write And Execute

Page 17: CS201- Introduction to Programming- Lecture 21

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

RaidRaidRedundant Array of Inexpensive DevicesRedundant Array of Inexpensive Devices

Page 19: CS201- Introduction to Programming- Lecture 21

Hot PlugHot Plug

Page 20: CS201- Introduction to Programming- Lecture 21

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

Unsigned Unsigned integerinteger

unsigned int i , j , k ;unsigned int i , j , k ;

Page 22: CS201- Introduction to Programming- Lecture 21

Left ShiftLeft ShiftA number 1A number 1

Shift leftShift left

1

10

Page 23: CS201- Introduction to Programming- Lecture 21

Right ShiftRight ShiftA number 10A number 10

Right shiftRight shift

10

01

Page 24: CS201- Introduction to Programming- Lecture 21

Left & Right Shift Left & Right Shift Operator Operator

<< shift left<< shift left >> shift right>> shift right

Page 25: CS201- Introduction to Programming- Lecture 21

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

1100

1100000011110011

Left shift

Right shift

11 11 00 00 00 11

00