Page 105 - CA_Blue( J )_Class9
P. 105

Write a program using conditional  operator to find the greatest number out of the two given
                  Program 19
                                 numbers.
                   1  class conditional

                   2  {
                   3      public static void main(String args[])
                   4      {

                   5          int a = 15, b = 56;
                   6          int grt;

                   7          grt = (a > b) ? a: b;
                   8          System.out.println(grt+" is greater");

                   9     }
                  10  }

                 You will get the following output:











                 5.3.8 Bitwise Operator
                 In Java, bitwise operators are used to manipulate individual bits of integer data types. These operators work
                 directly on the binary representations of numbers and are generally faster than arithmetic operations. Here
                 are the bitwise operators in Java:
                 •  AND (&): This operator compares each bit of two numbers and returns 1 only if both bits are 1.
                    Example:
                        int a = 5; // binary: 0101
                        int b = 3; // binary: 0011
                        int result = a & b; // result: 0001 (1 in decimal)
                 •  OR (|): This operator compares each bit of two numbers and returns 1 if either of the bits is 1.
                    Example:
                        int a = 5; // binary: 0101
                        int b = 3; // binary: 0011
                        int result = a | b; // result: 0111 (7 in decimal)
                 •  XOR (^): This operator compares each bit of two numbers and returns 1 only if the bits are different (i.e., one
                    bit is 1 and the other is 0).
                        int a = 5; // binary: 0101
                        int b = 3; // binary: 0011
                        int result = a ^ b; // result: 0110 (6 in decimal)
                 •  NOT (~): This is a unary operator that inverts all the bits of a number (turns 1 to 0 and 0 to 1).
                    Example:
                        int a = 5; // binary: 0101
                        int result = ~a; // result: 1010 (in decimal: -6, due to two's complement representation)


                                                                                               Operators in Java   103
   100   101   102   103   104   105   106   107   108   109   110