Page 155 - Cs_withBlue_J_C11_Flipbook
P. 155

The following program demonstrates the use of the shift operators:

























                 The output of the preceding program is as follows:

                 Enter a number
                 4
                 Using different shift operators, we get the following result:
                 1.  >> 2 : 1
                 2.  << 3 : 32
                 3.  >>> 4 : 0

                     7.3 PRECEDENCE OF OPERATORS
                 The precedence of operators determines the order in which the operators are going to be executed in an expression.

                 The following table shows the precedence of operators in Java, the higher an operator appears in the table, the higher its
                 precedence.

                         S.No.                    Operators                               Precedence

                           1        postfix unary                         cal++, cal--
                           2        unary including prefix, logical NOT   ++cal, --cal, +cal, -cal, ~, !
                           3        multiplication, division and modulus  *, /, %
                           4        addition and subtraction              +, -

                           5        Shift                                 <<, >>, >>>
                           6        Relational                            <, >, <=, >=
                           7        equality, non-equality                ==, !=
                           8        bitwise AND                           &
                           9        bitwise exclusive OR                  ^
                           10       bitwise inclusive OR                  |

                           11       logical AND                           &&
                           12       logical OR                            ||
                           13       Ternary                               ? :
                           14       assignment including shorthand        =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=




                                                                                                                       153
                                                                                               Variables and Expressions  153
   150   151   152   153   154   155   156   157   158   159   160