Last Updated: 28 May, 2022

Operators are special symbols that perform the specified operations. Each operator is responsible to perform some specific operation on variables or values and return a result. The operators are classified and listed according to precedence order.

Java supports the following types of operators:

- Arithmetic Operator
- Assignment Operator
- Bitwise Operator
- Logical Operator
- Relational Operator
- Ternary Operator
- Unary Operator

Bitwise operators work on binary digits (0 or 1) or bits of input values. They can be used with any integral type such as byte, short, int, and long.

Bit is a smallest unit of data, each bit contains a single value that is either 0 or 1.

In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

There are the following types of Bitwise Operators in Java they perform bit level operations.

- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise Complement (~)
- Left Shift (<<)
- Right Shift (>>)
- Unsigned Right Shift (>>>)

See the give table of Bitwise Operators:

Operators | Symbol | Uses |
---|---|---|

Bitwise AND | & | op1 & op2 |

Bitwise OR | | | op1 | op2 |

Bitwise XOR | ^ | op1 ^ op2 |

Bitwise Compliment | ~ | ~op |

Bitwise left shift | << | op1 << op2 |

Bitwise right shift | >> | op1 >> op2 |

Unsigned Right Shift Operator | >>> op >>> | number of places to shift |

Let's understand the bitwise operators in details with examples.

Bitwise AND is a binary operator denoted by the symbol **&**. It returns 1 if both bits are 1, else returns 0. For example,

a = 6, Binary Value of 6: 0110

b = 5, Binary Value of 5: 0101

a | b | a & b |
---|---|---|

0 | 0 | 0 |

1 | 1 | 1 |

1 | 1 | 1 |

0 | 0 | 0 |

Let's understand bitwise AND (&) operator in a Java program.

Example 1 : Bitwise AND (&) Operator

Output

a & b = 4

Bitwise OR is also an binary operator denoted by the symbol **|**. It returns 1 if any bit is 1, else returns 0. For example,

a = 6, Binary Value of 6: 0110

b = 7, Binary Value of 7: 0111

a | b | a | b |
---|---|---|

0 | 0 | 0 |

1 | 1 | 1 |

1 | 1 | 1 |

0 | 1 | 1 |

Let's understand bitwise OR (|) operator in a Java program.

Example 2 : Bitwise OR (|) Operator

Output

a | b = 7

Bitwise XOR is also an binary operator denoted by the symbol **^** (pronounced as caret). It returns 1 if both the bits are same, else returns 0. For example,

a = 6, Binary Value of 6: 0110

b = 7, Binary Value of 7: 0111

a | b | a ^ b |
---|---|---|

0 | 0 | 0 |

1 | 1 | 1 |

1 | 1 | 1 |

0 | 1 | 1 |

Let's understand bitwise XOR (^) operator in a Java program.

Example 3 : Bitwise XOR (^) Operator

Output

a ^ b = 1

Bitwise Complement is an unary operator (works with only one operand), denoted by the symbol **~** (pronounced as tilde). It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means It changes binary digits 1 to 0 and 0 to 1. For example,

a = 6, Binary Value of 6: 0110

a | ~a |
---|---|

0 | 1 |

1 | 0 |

1 | 0 |

0 | 1 |

Let's understand bitwise Complement (~) operator in a Java program.

Example 4 : Bitwise Complement (~) Operator

Output

~a = -7

Shift operators are used to shifting the bits either right or left direction. We use shift operators if we divide or multiply any number by 2.

Syntax of Shift Operator:

variable shift_op_symbol number_of_places_to_shift;

For example,

Java supports mainly three types of shift operators:

- Signed Left Shift (<<)
- Signed Right Shift (>>)
- Unsigned Right Shift (>>>)

Please note that Java does not support the unsigned left shift operator (<<<).

That's all guys, hope this Java article is helpful for you.

Happy Learning... 😀

If you have any query or suggestion regarding this artical please share with us

`feedback@javabytechie.com`