Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0035 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