# Java Bitwise Operators

Learn about the Java bitwise operators, their usages, and examples. Submitted by Nidhi Last updated : January 02, 2024

## Bitwise Operators in Java

Java bitwise operators are used to manipulating bits of a number. We can use bitwise operators with any integral datatype like byte, short, int, long.

## Types of Java Bitwise Operators

There are the following types of bitwise operators are used in Java,

1. Java Bitwise AND (&)
2. Java Bitwise OR (|)
3. Java Bitwise XOR (^)
4. Java Bitwise Complement (~)
5. Java Bitwise Left Shift (<<)
6. Java Bitwise Right Shift (>>)
7. Java Bitwise Unsigned Right Shift Operator (>>>)

## Java Bitwise Operators: Tabular Representation

OperatorSymbolExample
Bitwise AND&Operand1 & Operand2
Bitwise OR|Operand1 | Operand2
Bitwise XOR^Operand1 ^ Operand2
Bitwise Complement~~Operand1
Bitwise Left Shift<<Operand1 << Operand2
Bitwise Right Shift>>Operand1 >> Operand2
Bitwise Unsigned Right Shift Operator>>>Operand1 >>> Operand2

### 1. Java Bitwise AND Operator (&)

This is a binary operator, which is denoted by symbol '&'. It performs bit by bit AND operation on given number, if both bits are 1, then output will be 1, otherwise it will 0.

Operand1Operand2Operand1 & Operand2
111
1 0 0
0 1 0
0 0 0

#### Example of Java Bitwise AND Operator

```public class Main {
public static void main(String[] args) {

int num1 = 5; // 0101
int num2 = 7; // 0111

int res = 0;

res = num1 & num2;

/*
0101
0111
====
0101  That is 5
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: 5
```

### 2. Java Bitwise OR Operator (|)

This is a binary operator, which is denoted by symbol '|'. It performs bit by bit OR operation on given number, if any bit is 1, then output will be 1, otherwise it will 0.

Operand1 Operand2 Operand1 | Operand2
1 1 1
1 0 1
0 1 1
0 0 0

#### Example of Java Bitwise OR Operator

```public class Main {
public static void main(String[] args) {

int num1 = 5; // 0101
int num2 = 7; // 0111

int res = 0;

res = num1 | num2;

/*
0101
0111
====
0111  That is 7
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: 7
```

### 3. Java Bitwise XOR Operator (^)

This is a binary operator, which is denoted by symbol '^'. It performs bit by bit XOR operation on given number, if both bits are different, then output will be 1, otherwise it will 0.

Operand1 Operand2 Operand1 | Operand2
1 1 0
1 0 1
0 1 0
0 0 0

#### Example of Java Bitwise XOR Operator

```public class Main {
public static void main(String[] args) {

int num1 = 5; // 0101
int num2 = 7; // 0111

int res = 0;

res = num1 ^ num2;

/*
0101
0111
====
0010  That is 2
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: 2
```

### 4. Java Bitwise Complement Operator (~)

This is a unary operator, which is denoted by symbol '~'. It performs bit by bit 1's complement operation on given number. It inverted the bits.

Operand ~Operand
1 0
0 1

### 5. Java Bitwise Left Shift Operator (<<)

It is a binary operator, which shifts the number bits to the left in given number. The left-shift operator multiplies the number of specified bits of power of 2 with the given number.

#### Example of Java Bitwise Left Shift Operator

```public class Main {
public static void main(String[] args) {

byte num = -5;
byte res = 0;

res = (byte)(num << 3);

/*
res = -5 * (2*2*2)
res = -40
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: -40
```

### 6. Java Bitwise Right Shift Operator (>>)

It is a binary operator, which shifts the number bits to the right in given number. The right-shift operator divides the number of specified bits of power of 2 with the given number.

#### Example of Java Bitwise Right Shift Operator

```public class Main {
public static void main(String[] args) {

byte num = -64;
byte res = 0;

res = (byte)(num >> 3);

/*
res = -64 / (2*2*2)
res = -8
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: -8
```

### 7. Java Bitwise Unsigned Right Shift Operator (>>>)

It is a binary operator, which shifts the number bits to the right in given number. The unsigned right-shift operator divides the unsigned number of specified bits of power of 2 with the given number.

#### Example of Java Bitwise Unsigned Right Shift Operator

```public class Main {
public static void main(String[] args) {

byte num = 64;
byte res = 0;

res = (byte)(num >>> 3);

/*
res = 64 / (2*2*2)
res = 8
*/

System.out.println("Result: " + res);
}
}
```

Output:

```Result: 8
```