Bit Operators, Logical Operators - Introduction to Programming - Lecture Slides, Slides of Computer Programming

Bit operators, Bit manipulation, Logical Operators, Bit manipulation operators, Bit wise AND Operator, Bit wise Exclusive OR Operator, Right Shift Operator, NOT Operator are the key points of this lecture.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 21
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Bit Operators, Logical Operators - Introduction to Programming - Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 21

Today’s Lecture

 Bit manipulation

 Bit operators

Bit manipulation operators

& Bitwise AND Operator

| Bitwise OR Operator

^ Bitwise Exclusive OR Operator

~ NOT Operator

<< Left Shift Operator

Right Shift Operator

Bitwise AND Operator

Truth table for AND operation

Bit1 Bit2 Bit1 & B it 1 1 1 1 0 0 0 1 0 0 0 0

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 B A|B 1 1 1 1 0 1 0 1 1 0 0 0

Bitwise OR Operator

Example 2

x = 8 | 1

1 0 0 0 0 0 0 1


1 0 0 1

Hence the result x = 9

Bitwise Exclusive OR Operator

Truth table for Exclusive OR operation

A B A^B 1 1 0 1 0 1 0 1 1 0 0 0

NOT Operator

Truth table for NOT operator

A ~A 0 1 1 0

NOT Operator

x = 8

~ ( 1000 ) = 0111

= 7

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

Hot Plug

Example

Swapping two integers without a temporary storage

unsigned int a = 12 ; unsigned int b = 8 ;

a = a ^ b ; b = b ^ a ; a = a ^ b ;