Java provides a number of bitwise operators. They can be applied to integer types including char type. They are named bitwise because they act on individual bits of their operands.
For example purpose, assume three integer variables X, Y and Z hold value 45, 25 and 35 respectively.
Operator  Operator Name  Type  Example 

~  bitwise NOT  unary  ~X = 46 
&  bitwise AND  binary  X & Y = 9 
  bitwise OR  binary  X  Y = 61 
^  bitwise XOR  binary  X ^ Y = 52 
<<  Left Shift  binary  X << 2 = 180, Z << 2 = 140 
>>  Right Shift  binary  X >> 2 = 11, Z >> 2 = 9 
>>>  Unsigned Right Shift  binary  X >>> 2 = 11, Z >>> 2 = 1073741815 
Variable  Value  bitpattern 

X  45  00000000 00000000 00000000 00101101 
Y  25  00000000 00000000 00000000 00011001 
Z  35  11111111 11111111 11111111 11011101 
Note that all the binary bitwise operators have compound assignment forms which are just shortcuts and slightly optimised version of their normal forms.
Operator  Use  Equivalent to 

&=  X &= Y  X = X & Y 
=  X = Y  X = X  Y 
^=  X ^= Y  X = X ^ Y 
<<=  X <<= Y  X = X << Y 
>>=  X >>= Y  X = X >> Y 
>>>=  X >>>= Y  X = X >>> Y 
The Bitwise Logical Operators
There are 4 bitwise logical operators: &, , ^ and ~ . The Table below shows the bitwise operations for individual bits.
X  Y  X & Y  X  Y  X ^ Y  ~X 

0  0  0  0  0  1 
0  1  0  1  1  1 
1  0  0  1  1  0 
1  1  1  1  0  0 
This table for bitwise logical operators can be easily extended to numbers(so that operation happens on multiple bits).
Bitwise NOT
It is a unary operator meaning it takes only one operand. It inverts all the bits of its operand i.e. 0 becomes 1 and 1 becomes 0. For example: when X=45 then ~X = 46.
~ 00000000 00000000 00000000 00101101
11111111 11111111 11111111 11010010
It is also called bitwise complement operator.
Bitwise AND
It is a binary operator meaning it takes two operands. Whenever both the operands are 1, output is 1, else 0. For example: if X=45 and Y=25 then X&Y = 9.
00000000 00000000 00000000 00101101
&
00000000 00000000 00000000 00011001
00000000 00000000 00000000 00001001
Bitwise OR
It is also a binary operator. Whenever any of the operands is 1, the output is 1, else 0. For example: if X=45 and Y=25 then XY = 61.
00000000 00000000 00000000 00101101

00000000 00000000 00000000 00011001
00000000 00000000 00000000 00111101
Bitwise XOR / ExclusiveOR
It is yet another binary operator. Whenever exactly one of the operands is 1, the output is 1, else 0. For example: if X=45 and Y=25 then X^Y = 52.
00000000 00000000 00000000 00101101
^
00000000 00000000 00000000 00011001
00000000 00000000 00000000 00110100
1 2 3 4 5 6 7 8 9 10 
// Demonstration of bitwise logical operator public class Bitwise_logical_operators { public static void main(String[] args) { int X = 45, Y=25; System.out.println("~X = " + ~X); System.out.println("X & Y = " + (X&Y)); System.out.println("X  Y = " + (XY)); System.out.println("X ^ Y = " + (X^Y)); } } 
The output is:
1 2 3 4 
~X = 46 X & Y = 9 X  Y = 61 X ^ Y = 52 
The Left Shift
This operator shifts all the bits of an operand to left by specified number of times. It has the general form:
operand << num
where the operand is leftshifted by specified number num. For a left shift by 1, the leftmost bit is lost and a 0 is attached to rightmost bit position. For example, if X=45, then X<<1 = 90
X = 00000000 00000000 00000000 00101101
X << 1 = 0000000 00000000 00000000 01011010
i.e all the bits were shifted left by one position, the leftmost 0 falls off and a 0 appears on the rightmost bit position.
Similarly for Y=25, Y<<3 = 200
Y = 00000000 00000000 00000000 00011001
Y << 3 = 00000000 00000000 00000000 11001000
i.e. all the bits were shifted left by 3 positions, 3 leftmost bits fall off and 3 extra 0’s appear at the right.
This works the same way for negative numbers too. For Z = 35, Z<<2 = 140.
Z = 11111111 11111111 11111111 11011101
Z << 2 = 11111111 11111111 11111111 01110100
Here all the bits were shifted left by 2 positions, 2 leftmost 1’s fall off and 2 extra 0’s are attached at the right.
There are two things to note here:
 Leftshift by npositions is equivalent to multiplication by 2^{n}. When X was leftshifted by 1 position it became 90 from 45 i.e. multiplied by 2, when Y was leftshifted by 3 positions it became 200 from 25 i.e. multiplied by 2^{3} and when Z was leftshifted by 2 positions it became 140 from 35 i.e. multiplied by 2^{2}.
 For int type, the operand bits are lost once they shift past bit position 31, and for long type they are lost once they shift past position 63.
Be careful if you want to use left shift for multiplication by 2^{n}, if the new number doesn’t fit into the used datatype(say int) range, the result is new number modulo datatype range.
Java promotes byte and short values to int in an expression. So the leftshift is performed on 4bytes rather than 1byte for byte and 2bytes for short. The result of the leftshift on byte and short is also an int and they must be typecasted to suitable types.
1 2 3 4 5 6 7 8 9 10 11 
// Demonstration for leftshift public class Left_shift { public static void main(String[] args) { byte b1 = 32; int i1 = (b1 << 3); byte b2 = (byte)(b1 << 3); System.out.println("b1 = " + b1); System.out.println("i1 = " + i1 + " and b2 = " + b2); } } 
It produces the output
1 2 
b1 = 32 i1 = 256 and b2 = 0 
The binary representation of b1=32 in example above is 00100000 . Inside the expression b1<<3, b1 is promoted to int and it becomes 00000000 00000000 00000000 00100000. On leftshifting by 3 positions, it becomes:00000000 00000000 00000001 00000000. When it is assigned to integer variable i1 it holds its value as 256. Since assigning an int variable to a byte variable requres explict casting, we cast it to byte type and so top 3 bytes are dropped and the bitpattern assigned to b1 is 00000000 which has value 0.
The Right Shift
This operator shifts all the bits of an operand to right by specified number of times. It has the general form:
operand >> num
where the operand is rightshifted by specified number num. For a right shift by 1, the rightmost bit is lost and another extra bit is added at the leftmost position. If the operand is positive, the bit added is 0 and if the operand is negative, the bit added is 1. There is another way to thing about it.
 If the number was positive the leftmost bit was 0, So shift the bits by specified amount and add those many 0’s to the left.
 Similarly if the number was negative the leftmost bit would have been 1, so shift the bits by specified amount and add those many 1’s to the left.
This is called sign extension and it preserves the sign of a number. i.e positive numbers remain positive and negative numbers remain negative after rightshift. That’s why >> is also called signed rightshift. (You will learn about unsigned right shift shortly)
For example, if X=45, then X>>1 = 22
X = 00000000 00000000 00000000 00101101
X >> 1 = 0000000 00000000 00000000 00010110
i.e all the bits were shifted right by one position, the rightmost 1 falls off and a 0 appears on the leftmost bit position since the number is positive.
Similarly for Y=25, Y>>3 = 3
Y = 00000000 00000000 00000000 00011001
Y >> 3 = 00000000 00000000 00000000 00000011
i.e. all the bits were shifted right by 3 positions, 3 rightmost bits fall off and 3 extra 0’s appear to the left.
This works the same way for negative numbers too. For Z = 35, Z>>2 = 9.
Z = 11111111 11111111 11111111 11011101
Z >> 2 = 11111111 11111111 11111111 11110111
Here all the bits were shifted right by 2 positions, 2 rightmost bits fall off and 2 extra 1’s are attached to the left since the number is negative.
One intresting thing to note is that the rightshift of 1 by any number is 1. This happens because the signextension keeps bringing 1’s to the leftmost position leaving the bitpattern(all 1’s) unchanged. For S = 1, S >> 5 = 1, and S >> 20 = 1.
S = 11111111 11111111 11111111 11111111
S >> 5 = 11111111 11111111 11111111 11111111
S >> 20 = 11111111 11111111 11111111 11111111
i.e. the bitpatterns don’t change at all; leaving number unchanged.
The Unsigned RightShift
This operator(>>>) works similar to the right shift operator (>>) but instead of filling in the higherorder bits by 0’s or 1’s depending on the sign of the number, it always fills them by 0’s. Its general form is:
operand >>> num
where the operand is rightshifted by specified number num. For a right shift by 1, the rightmost bit is lost and a 0 (always) is added to leftmost bit position.
For example, if X=45, then X >>> 1 = 22
X = 00000000 00000000 00000000 00101101
X >>> 1 = 0000000 00000000 00000000 00010110
i.e all the bits were shifted right by one position, the rightmost 1 falls off and a 0 appears on the leftmost bit position since the number is positive.
Similarly for Y=25, Y >>> 3 = 3
Y = 00000000 00000000 00000000 00011001
Y >>> 3 = 00000000 00000000 00000000 00000011
i.e. all the bits were shifted right by 3 positions, 3 rightmost bits fall off and 3 extra 0’s appear to the left.
However, this works differently for the negative numbers. For Z = 35, Z >>> 2 = 1073741815.
Z = 11111111 11111111 11111111 11011101
Z >>> 2 = 00111111 11111111 11111111 11110111
Here all the bits were shifted right by 2 positions, 2 rightmost bits fall off and 2 extra 0’s are attached to the left even if this number is negative.
This operator doesn’t care about the sign of the number on which it is applied and hence, it has the name unsigned right shift.