Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Lecture 21

2 Today’s Lecture Bit manipulation Bit operators

3 Logical Operators AND && OR ||

4 Bit manipulation operators
& Bitwise AND Operator | Bitwise OR Operator ^ Bitwise Exclusive OR Operator ~ NOT Operator << Left Shift Operator >> Right Shift Operator

5 Truth table for AND operation
Bitwise AND Operator Truth table for AND operation Bit1 Bit2 Bit1 & Bit2 1

6 Bitwise AND Operator Example
………… 12 = & 8 = _______________ Hence x = 12 & 8 = 8

7 Example #include <iostream.h> main ( ) { int number = 12 ;
if ( number & 0x8 ) cout << "Bit number four is set" << endl ; else cout << "Bit number four is not set" << endl ; }

8 Truth table for OR operation
Bitwise OR Operator Truth table for OR operation A B A|B 1

9 Bitwise OR Operator Example 1
| _____________ Hence the result x = 12

10 Bitwise OR Operator Example 2
| _____________ Hence the result x = 9

11 Bitwise Exclusive OR Operator
Truth table for Exclusive OR operation A B A^B 1

12 Example: Exclusive OR Operator
^ _____________ Result x = 9 ^ X = 9 ^ 1 _____________ Result x = 8

13 Truth table for NOT operator
1

14 NOT Operator x = 8 ~ ( 1000 ) = 0111 = 7

15 Bit Flags

16 Read Write And Execute

17 Exclusive OR Operator Example
unsigned int a , b , c ; a = 112 ; b = 32 ; c = a ^ b ; c = ( a ^ b ) ^ b ; the result is a c = ( a ^ b ) ^ a ; the result is b

18 Redundant Array of Inexpensive Devices
Raid Redundant Array of Inexpensive Devices

19 Hot Plug

20 Example Swapping two integers without a temporary storage
unsigned int a = 12 ; unsigned int b = 8 ; a = a ^ b ; b = b ^ a ;

21 Unsigned integer unsigned int i , j , k ;

22 Left Shift A number 1 Shift left 1 10

23 Right Shift A number 10 Right shift 1 1

24 Left & Right Shift Operator
<< shift left >> shift right

25 Left & Right Shift Operator
Unsigned int i = 4 ; i << 1 ; shift left i >> 1 ; shift right

26 Left shift 1 1 1 1 Right shift 1 1 1 1


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google