refresh java logo

BitWise and BitShift Operators

Bitwise and Bit shift operators operates on bits 0 and 1. These operators can be used with integer type variables only like byte, short, int and long.

Bitwise & operator performs bitwise AND operation on bits. It returns 1 if both bit's are 1 else returns 0. Bitwise AND operator is a binary operator because it operates on two operands.

What is the difference between & and && operator ?

The & operator performs bitwise AND operation on bit representation of both number and returns an integer number as result while && operator performs operation on two boolean expressions and returns boolean value(true or false) as result.

Bitwise | operator performs bitwise OR operation on bits. It returns 1 if any of the bit's is 1 else returns 0. It is also a binary operator.

What is the difference between | and || operator ?

The | operator performs bitwise OR operation on bit representation of both number and returns an integer number as result while || operator performs operation on two boolean expressions and returns boolean value(true or false) as result.

Bitwise ^ operator performs bitwise exclusive OR operation on bits. It returns 1 if both bit's are different else returns 0. It is also a binary operator.

The unary bitwise complement operator ~ inverts a bit pattern. It converts "0" as "1" and "1" as "0". For example a byte data type contains 8 bits, using this operator with a byte variable with value whose bit pattern is "00000010" would be changed as "11111101".

     00110101            00110101            00110101            ~ 00000010  
& 01001111 | 01001111 ^ 01001111 ----------
---------- ---------- ---------- 11111101
00000101 01111111 01111010

Bit shift operators shifts the bits left or right depending on operators used. The bit pattern is given by the left-hand operand, and the number of positions to shift by is given by the right-hand operand.

The signed left shift operator "<<" shifts a bit pattern to the left by the number of positions given in the right side of this operator. It places 0 in right side for every shift. Java stores number's in 2's complement form, in which most significant bit(leftmost bit) is used for sign bit. Sign bit is used as 0 for positive number and 1 for negative number that is why using this operator a negative number can become a positive if a 0 is shifted to sign bit and a positive number can become negative number if a 1 is shifted to sign bit, these conditions are indications of underflow and overflow respectively. It is an unary operator.

The signed right shift operator ">>" shifts a bit pattern to the right by the number of positions given in the right side of this operator. The signed right shift operator places 0(for +ve number) or 1(for -ve number) in left side for every shift.

The unsigned right shift operator ">>>" shifts a bit pattern to the right by the number of positions given in the right side of this operator and places a 0 into the leftmost position, no matter the number is negative or positive. This operator always returns a positive number.

      00110101 << 2         00110101 >> 2         10110101 >> 2          00110101 >>> 2 
-------------- -------------- --------------- ---------------
11010100 00001101 11101101 00001101

What is the difference between signed and unsigned right shift operator ?

The only difference between signed and unsigned right shift operator is, signed right shift operator places 0 or 1 into the leftmost position depending on the number is positive or negative while the unsigned right shift operator always places 0, no matter number is positive or negative.

Though integer takes 32 bit's, but let's consider only last 8 bit's for below program.

Java Program of Bitwise and Bit Shift Operators

 class BitOperatorDemo 
  {
     public static void main(String[] args) 
      {
         int num1 = 10;   
         int num2 = 6;
         int result =0;
		  
         result = num1 & num2;   /* 00000010 = 00001010 & 00000110 */
         System.out.println("num1 & num2 = " + result );

         result = num1 | num2;  /* 00001110 = 00001010 | 00000110 */
         System.out.println("num1 | num2 = " + result );

         result = num1 ^ num2;  /* 00001100 = 00001010 ^ 00000110 */
         System.out.println("num1 ^ num2 = " + result );

         result = ~num1;  /* 11110101(in 2's complement form -11) = ~00001010 */
         System.out.println("~num1 = " + result );

         result = num1 << 2;  /* 00101000 = 00001010<<2 */
         System.out.println("num1 << 2 = " + result );

         result = num1 >> 2;  /* 00000010 = 00001010>>2 */
         System.out.println("num1 >> 2  = " + result );

         result = num1 >>> 2; /* 00000010 00001010>>>2 */
         System.out.println("num1 >>> 2 = " + result );
     }
  }					

Output:

num1 & num2 = 2
num1 | num2 = 14
num1 ^ num2 = 12
~num1 = -11
num1 << 2 = 40
num1 >> 2 = 2
num1 >>> 2 = 2

★★★
  • Bitwise AND operation doesn't mean multiplication of two number
  • Bitwise OR operation doesn't mean addition of two number.
  • Usually << operator increases the value by 2 times for every shift if it doesn't fall's in underflow or overflow condition.
  • If a number is positive, then the result of >> and >>> will be same.