Page 343 - Computer_Science_F5
P. 343

Computer Science  Table 5.10 shows the the summary of the truth table for bitwise operators:

           Table 5. 10: Truth table

                                    A B A&B A|B A^B ~A
                                                        0
                                        0
                                                               1
                                           0
                                                  0
                                    0
          FOR ONLINE READING ONLY
                                    0
                                                        1
                                                  1
                                           0
                                        0
                                                               0
                                    1   1  0      1     1      1
                                    1   1  1      1     0      0
           To  calculate  the  bitwise  XOR operation  between  two integers,  12 and  25, you
           do not need a Java program. Here is how you can do it manually, take the binary
           representations of each number:
              12 = 00001100
           ^ 25 = 00011001
                      00010101  . This is equal to 21, figure in decimal digits.

           Workout the Program Example 5.8 in any editor then compile and run the program
           in JDK using cmd.exe to see how the bitwise operators works:


                    Program Example 5.8:

            Java program to demonstrate bitwise operators


              public class BitwiseOPR{

                    public static void main(String[] args){
                        int x = 15;
                        int y = 33;

                        System.out.println(“x&y = “ + (x & y));
                        System.out.println(“x|y = “ + (x | y));
                        System.out.println(“x^y = “ + (x ^ y));
                        System.out.println(“~x = “ + ~x);
                            }

               }









                                                 334
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   334                                                     23/07/2024   12:34
   338   339   340   341   342   343   344   345   346   347   348