Bitwise Operator Assignments

Bitwise operator assignments combines the assignment with the bitwise operation. The following two statements are equivalent:

 

a = a >> 4; 
a >>= 4;  

The following two statements are equivalent:


a = a | b; 
a |= b;

The following program demonstrates the bitwise operator assignments:

public class Main {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;
    a |= 2;
    b >>= 2;
    c <<= 2;
    a ^= c;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);

  }
}
  

The output of this program is shown here:


a = 15
b = 0
c = 12
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.