This section contains the detail about the Bitwise Operator in java.

## Bitwise Operator

Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, and byte. These operators act upon the individual bits of their operands. Bitwise operator works on bits and perform bit by bit operation.They are summarized in the following  :

 Operator Result ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift Right >>> Shift Right  zero fill << Shift left &= Bitwise AND assignment \= Bitwise OR assignment ^= Bitwise exclusive OR assignment >>= Shift Right assignment >>>= Shift Right  zero fill assignment <<= Shift left assignment

The Bitwise operator is of the following types :

1. The Bitwise Logical Operator.

2. The Bitwise Shift Operator.

3. The Bitwise Assignment Operator.

### The Bitwise Logical Operator

The Bitwise Logical Operators are &, | , ^, and ~. Assume if a = 60; and b = 13; Now in binary format they will be as follows:

a = 0011 1100

b = 0000 1101

When we apply Bitwise logical operator to it, the result will be :

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

### The Bitwise Shift Operator

The Bitwise Shift Operator is of following type :

Left Shift :

The left shift operator , <<, shifts all of the bits in a value to the left a specified number of times. It has the general form :

`value<<num`

The 'num' specifies the number of positions the bits will shift to left. For each shift left, the high-order bit is shifted  out (and lost), and a zero bit is brought in on the right. For example :

```int a = 64; a = a<<2;```

The final value stored in 'a' is 256. Looking at the same operation in binary shows more clearly how this happens :

```0100 0000         64 <<2 1 0000 0000      256```

Right Shift :

The Right Shift operator >>, shifts all of the  bits in a value to the right a specified number of times. It's general form is shown here :

`value>>num`

Example :

```int a = 35; a = a >>2;  // a now contains 8```

Same operation in binary :

```00100011      35 >>2 00001000      8```

Each time you shift a value to the right , it divides that value by two-and discards any remainder. When  you are shifting right , the top (leftmost) bits exposed by the right shift are filled in with the precious contents of the top bit. This is called "sign extension" and serves to preserve the sign of negative numbers when you shift them right. For example :

`11111000     -8>>111111100     -4`

Unsigned right shift  :

The unsigned right shift operator shifts all of the  bits in a value to the right a specified number of times but it doesn't preserve the sign of the value like >>(Right shift) operator. This is known as unsigned shift. For this you use operator >>> which always shifts zeros into the high order bit.

For example :

int a = -1;

a = a >>> 24;

Same operation in binary form :

```11111111 11111111 11111111 11111111      -1 in binary as an int>>>2400000000 00000000 00000000 11111111      255 in binary as an int```

Bitwise Assignment Operator  :

All the binary assignment operator is combination of two things -the "equal to" operator and the "bitwise operator". For example,

`a = a >> 4;a >> = 4;`

are equivalent. Another example :

`a = a | b ;a | = b;`

Go to Topic «PreviousHomeNext»