Using the Bitwise Logical Operators

The following program demonstrates the bitwise logical operators:


public class Main {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = a | b;
    int d = a & b;
    int e = a ^ b;
    int f = (~a & b) | (a & ~b);
    int g = ~a & 0x0f;

    System.out.println(" a = " + a);
    System.out.println(" b = " + b);
    System.out.println(" a|b = " + c);
    System.out.println(" a&b = " + d);
    System.out.println(" a^b = " + e);
    System.out.println("~a&b|a&~b = " + f);
    System.out.println(" ~a = " + g);

  }
}  

Here is the output from this program:

  
 a = 1
 b = 2
 a|b = 3
 a&b = 0
 a^b = 3
~a&b|a&~b = 3
 ~a = 14
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.