Bitwise operators in Java manipulate individual bits of a number. Bitwise Operator in Java is used on integral types, byte, short, int and long. Java Bitwise Operators includes AND, OR, NOT, XOR, Left & Right Shift Operator, OR Operator, AND Operator, XOR Operator and Compliment Operator in Java. Unlike C language, Java has unsigned right shift bitwise operator.

The java Binary Operators such as Bitwise Shift Operator, Bitwise Complement Operator and Unsigned Right Shift Operators that performs the bit level manipulation. The Binary operators such as AND, OR, NOT, XOR are performed using the Bitwise Operators in java.

These Java Bitwise Operators are not commonly used in programming. Since Bitwise Operators performs at bit level, it is mainly used on low level programming, encryption, compression programming and short circuit programs. This operators saves a lot of space and can be processed quickly.

## Java Bitwise Operators types

In Java, there are seven types of bitwise operators.

- Bitwise OR
- Bitwise AND
- Bitwise XOR
- Bitwise Complement
- Bitwise Left Shift
- Bitwise Right Shift
- Bitwise Unsigned Right Shift

## Bitwise OR Operator ( | )

Bitwise OR is a binary Operator, denoted by ‘|’ (Single Pipe Character) operates on two operands. Binary OR operator converts two operands into binary format and then performs ‘OR’ operation. If one of the bits is 1, then it will return 1. If not, 0 will be returned.

### Explanation of Bitwise OR operator

```
12 1 1 0 0
10 1 0 1 0
__ ________
14 1 1 1 0
```

### Java Program for Bitwise OR Operator

```
package com.yawintutor;
public class BitwiseOR {
public static void main(String[] args) {
int a = 12; // 1 1 0 0
int b = 10; // 1 0 1 0
int c = a | b; // 1 1 1 0 -> 14
System.out.println("The result of Bitwise OR Operator of ( " + a + " | " + b + " ) is " + c);
}
}
```

### Output

`The result of Bitwise OR Operator of ( 12 | 10 ) is 14`

## Bitwise AND Operator ( & )

Bitwise AND is a binary Operator, denoted by ‘&’ (Single Ampersand Character) operates on two operands. Binary AND operator converts two operands to binary format and then performs ‘AND’ operation. If both of the bits are 1, it will return 1. If not, 0 will be returned.

### Explanation of Bitwise AND operator

```
12 1 1 0 0
10 1 0 1 0
__ ________
8 1 0 0 0
```

### Java Program for Bitwise AND Operator

```
package com.yawintutor;
public class BitwiseAND {
public static void main(String[] args) {
int a = 12; // 1 1 0 0
int b = 10; // 1 0 1 0
int c = a & b; // 1 0 0 0 -> 8
System.out.println("The result of Bitwise AND Operator of ( " + a + " & " + b + " ) is " + c);
}
}
```

### Output

`The result of Bitwise AND Operator of ( 12 & 10 ) is 8`

## Bitwise XOR Operator ( ^ )

Bitwise XOR is a binary Operator, denoted by ‘^’ (Single Carat Character) operates on two operands. Binary XOR operator converts two operands to binary format and then performs ‘XOR’ operation. If both of the bits are 1 or 0, then it returns 0. If not, it returns 1.

### Explanation of Bitwise XOR operator

```
12 1 1 0 0
10 1 0 1 0
__ ________
6 0 1 1 0
```

### Java Program for Bitwise XOR Operator

```
package com.yawintutor;
public class BitwiseXOR {
public static void main(String[] args) {
int a = 12; // 1 1 0 0
int b = 10; // 1 0 1 0
int c = a ^ b; // 0 1 1 0 -> 6
System.out.println("The result of Bitwise XOR Operator of ( " + a + " ^ " + b + " ) is " + c);
}
}
```

### Output

`The result of Bitwise XOR Operator of ( 12 ^ 10 ) is 6`

## Bitwise Complement Operator ( ~ )

Bitwise Complement is a binary Operator, denoted by ‘~’ (Single tilde Character) operates on single operand. Binary Complement operator converts a operand into binary format and then performs ‘NOT’ operation. If a bits is 1, then it returns 0. If it is 0, it returns 1.

### Explanation of Bitwise Complement operator

```
12 0 0 . . . 0 1 1 0 0
__ ___________________
-13 1 1 . . . 1 0 0 1 1
```

### Java Program for Bitwise Complement Operator

```
package com.yawintutor;
public class BitwiseNOT {
public static void main(String[] args) {
int a = 12; // 0 1 1 0 0
int c = ~a; // 1 0 0 1 1 -> -13
System.out.println("The result of Bitwise NOT Operator of ( ~" + a + " ) is " + c);
}
}
```

### Output

`The result of Bitwise NOT Operator of ( ~12 ) is -13`

## Bitwise Left Shift Operator ( << )

Bitwise Left Shift is a binary Operator, denoted by ‘<<‘ (two less than Character) operates on two operands. Binary left shift operator converts first operands into binary format and then performs Left Shift operation. If the operand is left shifted by n then the number is multiplied by 2 to the power n.

### Explanation of Bitwise Left Shift operator

```
0 0 1 1 0 0 12
0 1 1 0 0 0 (12 << 1) --- 12 * (2 *1) --- 24
1 1 0 0 0 0 (12 << 2) --- 12 * (2 *2) --- 48
```

### Java Program for Bitwise Left Shift Operator

```
package com.yawintutor;
public class BitwiseLeftShift {
public static void main(String[] args) {
int a = 12; // 0 0 1 1 0 0
int b = a << 1; // 0 1 1 0 0 0 -> 24 -> 12 * (2 * 1)
int c = a << 2; // 1 1 0 0 0 0 -> 48 -> 12 * (2 * 2)
System.out.println("The result of Bitwise Left Shift Operator of ( " + a + " << " + 1 + " ) is " + b);
System.out.println("The result of Bitwise Left Shift Operator of ( " + a + " << " + 2 + " ) is " + c);
}
}
```

### Output

```
The result of Bitwise Left Shift Operator of ( 12 << 1 ) is 24
The result of Bitwise Left Shift Operator of ( 12 << 2 ) is 48
```

## Bitwise Unsigned Left Shift Operator ( <<< )

There is no Bitwise unsigned Left Shift in Java. The unsigned left shift is equalent to Binary left shift Operator.

## Bitwise Right Shift Operator ( >> )

Bitwise Right Shift is a binary Operator, denoted by ‘>>’ (two greater than Character) operates on two operands. Binary right shift operator converts first operands into binary format and then performs Right Shift operation. If the operand is right shifted by n then the number is divided by 2 to the power n.

### Explanation of Bitwise Right Shift operator

```
0 0 1 1 0 0 12
0 0 0 1 1 0 (12 >> 1) --- 12 / (2 *1) --- 6
0 0 0 0 1 1 (12 >> 2) --- 12 / (2 *2) --- 3
```

### Java Program for Bitwise Right Shift Operator

```
package com.yawintutor;
public class BitwiseRightShift {
public static void main(String[] args) {
int a = 12; // 0 0 1 1 0 0
int b = a >> 1; // 0 0 0 1 1 0 -> 6 -> 12 / (2 * 1)
int c = a >> 2; // 0 0 0 0 1 1 -> 3 -> 12 / (2 * 2)
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 1 + " ) is " + b);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 2 + " ) is " + c);
a = -12; // 1 1 0 1 0 0
b = a >> 1; // 1 1 1 0 1 0 -> -6 -> -12 / (2 * 1)
c = a >> 2; // 1 1 1 1 0 1 -> -3 -> -12 / (2 * 2)
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 1 + " ) is " + b);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 2 + " ) is " + c);
}
}
```

### Output

```
The result of Bitwise Right Shift Operator of ( 12 >> 1 ) is 6
The result of Bitwise Right Shift Operator of ( 12 >> 2 ) is 3
The result of Bitwise Right Shift Operator of ( -12 >> 1 ) is -6
The result of Bitwise Right Shift Operator of ( -12 >> 2 ) is -3
```

## Bitwise Unsigned Right Shift Operator ( >>> )

Bitwise Unsigned Right Shift is a binary Operator, denoted by ‘>>>’ (three greater than Character) operates on two operands. Binary unsigned right shift operator converts first operands into binary format and then performs Right Shift operation. If the operand is unsigned right shifted by n then the number is divided by 2 to the power n.

### Explanation of Bitwise Unsigned Right Shift operator

```
0 0 1 1 0 0 12
0 0 0 1 1 0 (12 >> 1) --- 12 / (2 *1) --- 6
0 0 0 0 1 1 (12 >> 2) --- 12 / (2 *2) --- 3
```

### Java Program for Bitwise Unsigned Right Shift Operator

```
package com.yawintutor;
public class BitwiseUnsignedRightShift {
public static void main(String[] args) {
int a = 12; // 0 0 1 1 0 0
int b = a >>> 1; // 0 0 0 1 1 0 -> 6 -> 12 / (2 * 1)
int c = a >>> 2; // 0 0 0 0 1 1 -> 3 -> 12 / (2 * 2)
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 1 + " ) is " + b);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >> " + 2 + " ) is " + c);
a = -12; // 1 1 . . . 1 1 0 1 0 0
b = a >>> 1; // 0 1 . . . 1 1 1 0 1 0 -> -6 -> -12 / (2 * 1)
c = a >>> 2; // 0 0 . . . 1 1 1 1 0 1 -> -3 -> -12 / (2 * 2)
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 1 + " ) is " + b);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 2 + " ) is " + c);
int d,e ;
a = -12; // 1 1 . . . 1 1 0 1 0 0
b = a >>> 28; // 0 0 . . . 0 0 1 1 1 1 -> -6 -> -12 / (2 * 1)
c = a >>> 29; // 0 0 . . . 0 0 0 1 1 1 -> -3 -> -12 / (2 * 2)
d = a >>> 30; // 0 0 . . . 0 0 0 0 1 1 -> -3 -> -12 / (2 * 2)
e = a >>> 31; // 0 0 . . . 0 0 0 0 0 1 -> -3 -> -12 / (2 * 2)
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 28 + " ) is " + b);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 29 + " ) is " + c);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 30 + " ) is " + d);
System.out.println("The result of Bitwise Right Shift Operator of ( " + a + " >>> " + 31 + " ) is " + e);
}
}
```

### Output

```
The result of Bitwise Right Shift Operator of ( 12 >> 1 ) is 6
The result of Bitwise Right Shift Operator of ( 12 >> 2 ) is 3
The result of Bitwise Right Shift Operator of ( -12 >>> 1 ) is 2147483642
The result of Bitwise Right Shift Operator of ( -12 >>> 2 ) is 1073741821
The result of Bitwise Right Shift Operator of ( -12 >>> 28 ) is 15
The result of Bitwise Right Shift Operator of ( -12 >>> 29 ) is 7
The result of Bitwise Right Shift Operator of ( -12 >>> 30 ) is 3
The result of Bitwise Right Shift Operator of ( -12 >>> 31 ) is 1
```