Computer Programming Part 1-Programming and Computer Architecture-Lecture Slides, Slides of Computer Architecture and Organization

Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: Computer, Programming, XOR, Operations, Bitwise, Operators, Signed, Integer, Types, Shift

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekraj
ekraj ๐Ÿ‡ฎ๐Ÿ‡ณ

4.1

(11)

123 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming
Bitwise operators, Left shit, right shift, OR, AND &
XOR operations
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Computer Programming Part 1-Programming and Computer Architecture-Lecture Slides and more Slides Computer Architecture and Organization in PDF only on Docsity!

Computer ProgrammingBitwise operators, Left shit, right shift, OR,

AND &

XOR operations

docsity.com

Bitwise Operators^ ๏‚—^ As their name suggests, the bitwiseoperators enable you to operate on aninteger variable at the bit level.^ โ—ฆ^ Can be applied to any type of integers:^ ๏‚–^

Signed ๏‚– Unsigned ๏‚– However they are usually applied to unsignedinteger types.

(^2) docsity.com

Example^ unsigned int number =163870;unsigned int result= number <<2;^ ๏‚—^ The left operand of the shift operator is the value to beshifted^ ๏‚—^ While the number of bit positions that the value is tobe shifted is specified by the right operand.

4

Decimal 16,387 in binary :^0

Shift left 2:

docsity.com

Example (Contd.)

5

Similarly if we apply shift right:Shift right 2:

unsigned int number =163870;unsigned int result= number >> 2;

docsity.com

Shifting Signed Integers^ ๏‚—^ As mentioned earlier bitwise shift operatorscan be applied to both signed and unsignedintegers.^ ๏‚—^ Right shift

on signed integer types can vary between different systems and it depends onyour compiler. ๏‚— In some cases it will fill

โ€˜0โ€™^ bits at the left to

fill in the vacated bit positions. ๏‚— In other cases the sign bit is propagated tothe right so

โ€˜1โ€™^ bit fills the vacated bit positions.

(^7) docsity.com

Shifting Signed Integers( Contd.)^ ๏‚—^ The reason for propagating the sign bit,where this occurs, is to maintainconsistency between a right shift and adivide operation.^ ๏‚—^ We will illustrate this with a variable oftype

char

, just to show how it works.

(^8) docsity.com

Logical Operations on Bit Patterns Operator

Description

-^

Bitwise complement operator.

This is a unary operator that

will invert the bits to its operand so 1 becomes 0 and 0becomes 1. &^

Bitwise AND operator which will AND the correspondingbits in its operands. If corresponding bits are both 1 thenresulting bit is 1, otherwise it is 0. ^^

Bitwise exclusive OR operator (XOR) if corresponding bitsare different e.g. 1 and 0 result will be 1 otherwise if bits aresame e.g. 0 and 0 result will be 0. |^

Bitwise OR operator. If corresponding bits are 0 and 0 theresulting bit will be 0, otherwise it is 1.

(^10) docsity.com

AND^ ๏‚—^ One major use of AND operation is forplacing 0s in one part of a bit patternwhile not disturbing the other part.^ ๏‚—^ For example 00001111^ โ—ฆ^ without knowing the second operand we cansay that the first four most significant bits willbe 0s^ โ—ฆ^ Moreover the four least significant bits will bea copy of the second operand.

2- (^11) docsity.com

AND(Contd.)^ ๏‚—^ This use of the AND operation is called^ masking

. ๏‚—^ Here one operand, called the

mask

,

determines which part of the otheroperand will affect the result. ๏‚— In case of AND operation, maskingproduces a result that is a partial replicaof the one operand, with 0s occupying thenonduplicated positions.

2- (^13) docsity.com

AND(Contd.)^ ๏‚—^ Such an operation is useful whenmanipulating a

bit map

,

โ—ฆ^ A string of bits in which each bit representsthe presence or absence of a particular object โ—ฆ^ We have encountered bit maps in context ofrepresenting images, where each bit isassociated with a pixel.

2- (^14) docsity.com

OR(Contd.)

(^11110000) OR 10101010^11111010

2- (^16) docsity.com

XOR^ ๏‚—^ A major use of XOR operation is in theforming of complement of a bit string^ ๏‚—^ XORing any byte with a mask of all 1sproduces the complement of the byte.

2- (^17) docsity.com

Quiz 61-Describe basics of a typical C++ environment .2- What two kinds of files hold you code in C++?3- Define types of scope variables with the help ofexample?4- What are Precedence? Write the Precedence table.5- Write code in C++ to explain the working of constvariable.6- Write a program that will display students gradesfrom A-F.7- Describe the difference between assignment andequality operator with help of examples.

(^19) docsity.com