Groovy – Bitwise Operators

  • Post author:
  • Post category:Groovy
  • Post comments:0 Comments
bitwise operators

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

Sr.NoOperator & 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.

pqp & qp | qp ^ q
00000
01011
11110
10011

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

Previous Page:-Click Here

Leave a Reply