Page 54 - Cs_withBlue_J_C11_Flipbook
P. 54

3
                                                     4
                                             5
              Equivalent decimal number is 1 × 2  + 1 × 2  + 1 × 2  = 56
              Example 2: 10 >> 2
              Binary equivalent of 10 is 1010. Representing as 8-bits and adding sign bit 0 at MSB for its positive sign, we get:
                                     0       0        0       0        1       0       1        0
              Shifting 2 places to right and filling zero to the vacant left positions:

                                     0       0        0       0        0       0       1        0
              Corresponding decimal value is 2.

                                                              Exceptions
               The shift value cannot be negative. Example: 5 << -2 is or 5 >> -2 is undefined.
               The shift value cannot be more than the size of the integer. Example: 2 >> 33 is undefined.

              Example 3: -10 >> 2

              Binary equivalent of 10 is 1010. Representing as 8 bits:
                                     0       0        0       0        1       0       1        0
              1’s complement is taken because of the negative sign:

                                     1       1        1       1        0       1       0        1

              2’s complement value is:
                                     1       1        1       1        0       1       1        0
              Shifting 2 positions to the right and replacing the vacant sign bits by 1 (-ve sign), we get:

                                     1       1        1       1        1       1       0        1
              1’s complement of 11111101 is:

                                     0       0        0       0        0       0       1        0
              2’s complement is:
                                     0       0        0       0        0       0       1        1

              Decimal equivalent result is -3.
              Example 4: 12 >>> 3

              Binary equivalent of 12 is 1100. Representing as 8 bits and adding sign bit 0 at MSB for its positive sign, we get:
                                     0       0        0       0        1       1       0        0
              Shifting 3 places to the right and filling the vacant left bits by 0, we get:

                                     0       0        0       0        0       0       0        1
              Corresponding decimal value is 1.

              2.1.5 Bitwise Logical Operators
              Bitwise logical operators perform logical operations on integers bit by bit. These operators are given below:
              a. Bitwise complement (˜)

              b. Bitwise AND (&)
              c. Bitwise OR (|)

              d. Bitwise XOR (∧)



                5252  Touchpad Computer Science-XI
   49   50   51   52   53   54   55   56   57   58   59