Groovy - Bitwise Operators



Groovy provides four bitwise operators. Following are the bitwise operators available in Groovy −

Sr.No Operator & Description
1

&

This is the bitwise “and” operator

2

|

This is the bitwise “or” operator

3

^

This is the bitwise “xor” or Exclusive or operator

4

~

This is the bitwise negation operator

Here is the truth table showcasing these operators.

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

The following code snippet shows how the various operators can be used.

class Example { 
   static void main(String[] args) { 
      int a = 00111100; 
      int b = 00001101; 
      int x;
		
      println(Integer.toBinaryString(a&b)); 
      println(Integer.toBinaryString(a|b)); 
      println(Integer.toBinaryString(a^b)); 
		
      a=~a; 
      println(Integer.toBinaryString(a)); 
   } 
}

When we run the above program, we will get the following result. It can be seen that the results are as expected from the description of the operators as shown above.

1001000000 
1001001001000001 
1001000000000001 
100100100100000
groovy_operators.htm
Advertisements