P15 Bitwise Operation

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

BITWISE OPERATIONS

2/15/2012

Bitwise Operators
Smallest memory element which we have used so far is byte. When you want to interact with the hardware, you need to handle bits. Programming languages-byte oriented Hardware-bit oriented This operators plays a vital role in the case of embedded systems.
2/15/2012 2

Bitwise Operators
Operates only on integer and character. Bitwise operators available in c.

2/15/2012

Bitwise operators
Bit numbering scheme for characters

Bit numbering scheme in integers

2/15/2012

Bitwise operators
Finding the binary equivalent of integer and character.
Construct a mask of width 16-bit. and the mask with integer value which has to be converted to binary. If the anded value is 1,then 1 is placed in the position, else 0 is placed in the position.

2/15/2012

Converting integer to binary


int i, k, andmask ; for ( i = 15 ; i >= 0 ; i-- ) { andmask = 1 << i ; k = n & andmask ; //n-number to be converted k == 0 ? printf ( "0" ) : printf ( "1" ) ; } 0000 0000 0000 0111 & 1000 0000 0000 0000
1000 0000 0000 0000 0100 0000 0000 0000 .. .. 0000 0000 0000 0001
2/15/2012

If n=7 0 will be placed for first 13 shifting

.. 0000 0000 0000 0111 & 0000 0000 0000 0100 0000 0000 0000 0111 & 0000 0000 0000 0010 0000 0000 0000 0111 & 0000 0000 0000 0001 0000 0000 0000 0111
6

Ones complement of number


On taking 1s complement ,1s in the number are changed to 0s in the number are changed to 1. ~ operator is used to perform 1s complement.

2/15/2012

Right Shift Operator


Represented by >>. Needs two operands. Shifts each bit in the left operand to the right. Number of places shifted depends on the number following the operator. After shifting, created blanks are filled with zeros
2/15/2012 8

Left shift Operator


Similar to the right shift operator, the only difference being that the bits are shifted to left. For each shifted bit,0 is added to the right of the number. Represented by << operator

2/15/2012

Bitwise AND operator


Represented by & operator. It operates on two operands. Compares the operands on bit by bit basis. Both the operands must be of same type Second operand is sometimes called as and mask.

2/15/2012

10

Bitwise AND operator

Importance of AND operator: Used to check whether a particular bit is on or off It is used to turn off a particular bit in a number

2/15/2012

11

Bitwise OR operator
Represented by | operator Similar to AND operator, operates on two operands. Used to put on a particular bit in a number.

2/15/2012

12

Bitwise XOR operator


Represented by ^ operator Exclusive OR operation OR returns 1,when anyone of the two bits are 1. Whereas XOR returns 1,only when one of the two bits is 1.

2/15/2012

13

I request Electronics and communication ENGINEERING students to visit my blog for more abhishek1ek.blogspot.com awhengineering.blogspot.com
2/15/2012

14

2/15/2012

15

You might also like