P15 Bitwise Operation

Embed Size (px)

Citation preview

  • 8/3/2019 P15 Bitwise Operation

    1/15

    2/15/20122/15/2012 11

    BITWISEBITWISE

    OPERATIONSOPERATIONS

  • 8/3/2019 P15 Bitwise Operation

    2/15

    2/15/20122/15/2012 22

    Bitwise OperatorsBitwise Operators

    Smallest memory element which we have

    used so far is byte.

    When you want to interact with thehardware, you need to handle bits.

    Programming languages-byte oriented

    Hardware-bit oriented This operators plays a vital role in the case

    of embedded systems.

  • 8/3/2019 P15 Bitwise Operation

    3/15

    2/15/20122/15/2012 33

    Bitwise OperatorsBitwise Operators

    Operates only on integer and character.

    Bitwise operators available in c.

  • 8/3/2019 P15 Bitwise Operation

    4/15

    2/15/20122/15/2012 44

    Bitwise operatorsBitwise operators

    Bit numbering

    scheme for

    characters

    Bit numberingscheme in integers

  • 8/3/2019 P15 Bitwise Operation

    5/15

    2/15/20122/15/2012 55

    Bitwise operatorsBitwise 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.

  • 8/3/2019 P15 Bitwise Operation

    6/15

    2/15/20122/15/2012 66

    Converting integer to binaryConverting integer to binaryint i, k, andmask ;

    for ( i = 15 ; i >= 0 ; i-- )

    {

    andmask = 1

  • 8/3/2019 P15 Bitwise Operation

    7/15

    2/15/20122/15/2012 77

    Ones complement of numberOnes 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.

  • 8/3/2019 P15 Bitwise Operation

    8/15

    2/15/20122/15/2012 88

    Right Shift OperatorRight 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

  • 8/3/2019 P15 Bitwise Operation

    9/15

    2/15/20122/15/2012 99

    Left shift OperatorLeft 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

  • 8/3/2019 P15 Bitwise Operation

    10/15

    2/15/20122/15/2012 1010

    Bitwise AND operatorBitwise AND operator

    Represented by & operator.

    It operates on two operands.

    Compares the operands on bit by bitbasis.

    Both the operands must be of same type

    Second operand is sometimes called asand mask.

  • 8/3/2019 P15 Bitwise Operation

    11/15

    2/15/20122/15/2012 1111

    Bitwise AND operatorBitwise 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

  • 8/3/2019 P15 Bitwise Operation

    12/15

    2/15/20122/15/2012 1212

    Bitwise OR operatorBitwise OR operator

    Represented by | operator

    Similar to AND operator, operates on two

    operands. Used to put on a particular bit in a number.

  • 8/3/2019 P15 Bitwise Operation

    13/15

    2/15/20122/15/2012 1313

    Bitwise XOR operatorBitwise XOR operator

    Represented by ^ operator

    Exclusive OR operation

    OR returns 1,when anyone of the two bitsare 1.

    Whereas XOR returns 1,only when one of

    the two bits is 1.

  • 8/3/2019 P15 Bitwise Operation

    14/15

    I request Electronics and communication

    ENGINEERING students to visit my blogfor

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.com2/15/20122/15/2012 1414

  • 8/3/2019 P15 Bitwise Operation

    15/15

    2/15/20122/15/2012 1515