Page 167 - ComputerScience_Class_11
P. 167

4.  5 ^ 3
                       5 = 0101 (In Binary)
                       3 = 0011 (In Binary)
                       Bit Operation of 5 ^ 3
                           0101
                       ^  0011
                           0110                             which is 6 in decimal
                 Ans.  6
                   5.  ∼47
                       47 = 101111 (In Binary)
                       Bitwise complement Operation of 47
                      ∼ 101111
                          010000 = 16 (In decimal)
                 Ans.  16

                   6.  12 | 25
                       12 = 01100 (In Binary)

                       25 = 11001 (In Binary)
                       Bitwise OR Operation of 12 and 25
                          01100
                       | 11001
                          11101  which is 29 in decimal
                 The following program demonstrates the use of bitwise operators:

                                 Write a program that takes two integer inputs from the user and demonstrates the use of
                  Program 4
                                 bitwise operators by printing their results.
                   1      import java.util.*;
                   2      class bitwise_operator

                   3      {
                   4          public static void main(String args[])
                   5          {
                   6              Scanner sc = new Scanner(System.in);
                   7              int a,b;
                   8              System.out.println("Enter 1st number");
                   9              a=sc.nextInt();
                  10              System.out.println("Enter 2nd number");
                  11              b=sc.nextInt();

                                   System.out.println("Using different bitwise operator, we get the
                  12                following result:");
                  13              System.out.println("a & b= "+(a & b));
                  14              System.out.println("a | b = "+(a | b));
                  15              System.out.println("~a = "+ ~a);
                  16              System.out.println("a ^ b = "+(a ^ b));
                  17          }
                  18      }




                                                                                            Variables and Expressions  165
   162   163   164   165   166   167   168   169   170   171   172