Java Bitwise AND and OR Exclusive OR and shift operators



If you have done some low level programming in C or other language, you must be familar with the bitwise operators. Java, too, provides bitwise operators. The table below explains the details of these bitwise operators Java Bitwise Operators


Operator Meaning
~ Complement
& bitwise AND
| bitwise OR
>> bitwise shift right
<< Bitwise shift left


The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types ( If you apply it to byte type - they expand it to integer - see explanation here , making every "0" a "1" and every "1" a "0".

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

The bitwise & operator performs a bitwise AND operation.

The bitwise ^ operator performs a bitwise exclusive OR operation.

The bitwise | operator performs a bitwise inclusive OR operation.

Let us take examples. First we need to understand how an integer is represented in Java. An integer in Java is 4 bytes long and has value ranging from -2,147,483,648 to 2,147,483,647 and is represented as 2's complement.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   Bitwise complement operation on integer
  4.  */
  5. class bitwise{
  6.  
  7. public static void main (String args[]) {
  8. int x = 5;
  9. int y = ~x ;
  10. System.out.println("5's complement is " + y);
  11. }
  12. }


If you compile and run this program, you will get the output as follows


C:\Program Files\Java\jdk1.7.0_17\bin>java bitwise
5's complement is 6




To understand why 5's complement is -6, you need to know about 2's complement. In binary term, 5 is represented as

00000000 00000000 00000000 00000101

Its complement will be

11111111 11111111 1111111 11111010

Which represents -6 in 2's complement.

The following code represents the rest of the bitwise operators

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   Bitwise operations on integer
  4.  */
  5. class bitwise3{
  6.  
  7. public static void main (String args[]) {
  8. int x = 0x0000FF00;
  9. int y = 0x00000FF0 ;
  10.  
  11. int z1 = x & y ; // That gives 0x00000F00
  12. int z2 = x | y ; // That gives 0x0000FFF0
  13. int z3 = x >> 4; // Right shift by 4 gives 0x00000FF0
  14. int z4 = x << 4; // Left shift by 4 gives 0x00FF0000
  15. int z5 = x ^ y ; // Exclusive OR gives 0x00000F0F
  16.  
  17.  
  18. System.out.println("x & y is " + z1);
  19. System.out.println("x | y is " + z2);
  20. System.out.println("x >> y is " + z3);
  21. System.out.println("x << y is " + z4);
  22. System.out.println("x ^ y is " + z4);
  23.  
  24. // Print Numbers in Hexadecimal
  25.  
  26. System.out.println("x & y is " + String.format("%08x", z1));
  27. System.out.println("x | y is " + String.format("%08x", z2));
  28. System.out.println("x >> y is " + String.format("%08x", z3));
  29. System.out.println("x << y is " + String.format("%08x", z4));
  30. System.out.println("x ^ y is " + String.format("%08x", z5));
  31. }
  32. }


If you compile and run this program, you will get the output as follows


x & y  is   3840
x | y  is   65520
x >> y  is   4080
x << y  is   1044480
x ^ y  is   1044480
x & y is 00000f00
x | y is 0000fff0
x >> y is 00000ff0
x << y is 000ff000
x ^ y is 0000f0f0