Programming Tutorials

Bitwise Operator Assignments

By: aathishankaran in Java Tutorials on 2007-02-07  

All of the binary bitwise operators have a shorthand form similar to that of the algebraic operators, which combines the assignment with the Bitwise operation. For example, the following two statements, which shift the value in a right by four bits, are equivalent:

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

Likewise, the following two statements, which result in a being assigned the bitwise expression a OR b, are equivalent:

a = a | b;
a |= b;

The following program creates a few integer variables and then uses the shorthand form of bitwise operator assignments to manipulate the variables:

class OpBitEquals {

  public static void main (String args[]) {

   int a = 1;
   int b = 2;
   int c = 3;

   a | = 4;
   b >>= 1;
   c <<= 1;
   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 = 3
b = 1
c = 6





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)