{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS201-21 - Introduction to Programming Lecture 21 Today’s...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 21 Today’s Lecture Bit manipulation Bit operators Logical Operators AND OR || && & | ^ ~ << >> Bit manipulation operators Bitwise AND Operator Bitwise OR Operator Bitwise Exclusive OR Operator NOT Operator Left Shift Operator Right Shift Operator Bitwise AND Operator Bit1 1 1 0 0 Truth table for AND operation Bit2 1 0 1 0 Bit1 & Bit2 1 0 0 0 Bitwise AND Operator Example ………… 2 2 2 2 12 = 1 100 & 8 ______________ 0 =1 0 0 3 2 1 0 _1 0 0 0 Hence x = 12 & 8 = 8 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 ; } Bitwise OR Operator Truth table for OR operation A 1 1 0 0 B 1 0 1 0 A|B 1 1 1 0 Bitwise OR Operator Example 1 X = 12 | 8 1 1 1 1 0 1 | 0 0 0 0 0 0 _____________ Hence the result x = 12 Bitwise OR Operator Example 2 x = 8 | 1 1 0 1 0 0 0 | 0 0 0 0 1 1 _____________ Hence the result x = 9 Bitwise Exclusive OR Operator Truth table for Exclusive OR operation A 1 1 0 0 B 1 0 1 0 A^B 0 1 1 0 X = 8 ^ 1 Example: Exclusive OR Operator 1 0 0 0 0 0^ 0 1 _____________ Result x = 9 1 001 X=9^1 0 Result x = 8 1 _____________ 0 0 ^0 0 1 0 NOT Operator Truth table for NOT operator A 0 1 ~A 1 0 NOT Operator x = 8 ~ ( 1000 ) = 0111 = 7 Bit Flags Read Write And Execute 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 Raid Redundant Array of Inexpensive Devices Hot Plug Swapping two integers without a temporary storage unsigned int a = 12 ; unsigned int b = 8 ; a = a ^ b ; b = b ^ a ; a = a ^ b ; Example Unsigned integer unsigned int i , j , k ; Left Shift A number 1 Shift left Right Shift A number 10 Right shift 10 01 Left & Right Shift Operator << shift left >> shift right Left & Right Shift Operator Unsigned int i = 4 ; i >> 1 ; shift right i << 1 ; shift left Left shift 10110001 Right shift 10110 001 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online