24 Jun Java Bitwise Operators
Java Bitwise Operators operate on individual bits of the operands. Let us learn about the type of Bitwise operators in Java. In this lesson, our example will have three variables,
1 2 3 4 5 |
int a = 6; int b = 7; int res; |
Bitwise Operators in Java are the following:
- Bitwise NOT,
- Bitwise AND,
- Bitwise OR,
- Bitwise X-OR
- Bitwise Right Shift Operator,
- Bitwise Right Shift Phil Zero Operator,
- Bitwise Left Shift Operator,
- Bitwise AND Assignment,
- Bitwise OR Assignment,
- Bitwise X-OR Assignment,
- Bitwise Right Shift Assignment Operator,
- Bitwise Right Shift Phil Zero Assignment Operator,
- Bitwise Left Shift Assignment Operator
Bitwise NOT
The Bitwise NOT operator inverts all the bits of its operand.
Symbol
1 2 3 |
~ |
Example
Let’s say a = 6, then
1 2 3 4 5 |
int a = 6; int res = ~a; System.out.println(res); |
Output
The output is 1,
1 2 3 |
1 |
Showing the above, with bits and the same result is visible,
Bitwise AND
The Bitwise AND operator gives 1, if both the operands are 1.
Symbol
1 2 3 |
& |
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; int res = a & b; System.out.println(res); |
Output
The output is 6,
1 2 3 |
6 |
Showing the above, with bits and the same result is visible,
Bitwise OR
The Bitwise OR operator gives 1, if either of the bit is 1.
Symbol
1 2 3 |
| |
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; int res = a | b; System.out.println(res); |
Output
The output is 7,
1 2 3 |
7 |
Showing the above, with bits and the same result is visible,
Bitwise X-OR
The Bitwise X-OR operator gives 1, if exactly one operand is 1.
Symbol
1 2 3 |
^ |
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; int res = a^b; System.out.println(res); |
Output
The output is 1,
1 2 3 |
1 |
Showing the above, with bits and the same result is visible,
Bitwise Right Shift Operator
The Bitwise Right Shift operator shifts bit to the right specified number of times.
Symbol
1 2 3 |
>> |
Example
1 2 3 4 5 |
int a = 6; int res = a>>1; System.out.println(res); |
Output
The output is 3,
1 2 3 |
3 |
Showing the above, with bits and the same result is visible. It shifts once,
Bitwise Right Shift Phil Zero Operator/ Unsigned Right Shift
The Bitwise Right Shift Zero operator does not preserve sign of original number and fills the new place with zero,
Symbol
1 2 3 |
>>> |
Example
1 2 3 4 5 |
int a = 6; int res = a>>>2; System.out.println(res); |
Output
The output is 1,
1 2 3 |
1 |
Bitwise Left Shift Operator
The Bitwise Left Shift operator shifts the bit to the left specified number of times in the bit sequence.
Symbol
1 2 3 |
<< |
Example
1 2 3 4 5 |
int a = 6; int res = a<<1; System.out.println(res); |
Output
The output is 4,
1 2 3 |
4 |
Showing the above, with bits and the same result is visible. It shifts once,
Bitwise AND Assignment
The Bitwise AND Assignment operator performs Bitwise AND and assigns the result to the variable. The Bitwise AND operator gives 1, if both the operands are 1.
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; a|= b System.out.println(a); |
Output
The output is 7,
1 2 3 |
7 |
Bitwise OR Assignment
The Bitwise OR Assignment operator performs a Bitwise OR and assigns the result to the variable. The Bitwise OR operator gives 1, if either of the bit is 1.
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; a | = b; System.out.println(a); |
Output
The value of the result is in the variable a. The output is 7,
1 2 3 |
7 |
Bitwise X-OR Assignment
The Bitwise X-OR Assignment operator performs Bitwise X-OR and assigns the result to the variable. The Bitwise X-OR operator gives 1, if exactly one operand is 1.
Example
1 2 3 4 5 6 |
int a = 6; int b = 7; a^ = b; System.out.println(a); |
Output
The value of the result is in the variable a. The output is 2,
1 2 3 |
2 |
Right Shift Assignment
The Bitwise Right Shift Assignment operator performs Bitwise Right Shift and assigns the result to the variable. The Bitwise Right Shift operator shifts bit to the right specified number of times.
Example
1 2 3 4 5 |
int a = 6; a>> = 1; System.out.println(a); |
Output
The value of the result is in the variable a. The output is 3,
1 2 3 |
3 |
Right Shift Phil Zero Assignment
The Bitwise Right Shift Phil Zero Assignment operator performs right shift phil zero and assigns the result to the variable.
The Bitwise Right Shift Zero operator does not preserve sign of original number and fills the new place with zero,
Example
1 2 3 4 5 |
int a = 6; a>>> = 2; System.out.println(a); |
Output
The value of the result is in the variable a. It shifts twice.
The output is 1,
1 2 3 |
1 |
Left Shift Assignment
The Bitwise Left Shift Assignment operator performs Bitwise Left Shift and assigns the result to the variable.
The Bitwise Left Shift operator shifts the bit to the left specified number of times in the bit sequence.
Example
1 2 3 4 5 |
int a = 6; a<< = 1; // equivalent to a = a<<1 System.out.println(a); |
Output
The value of the result is in the variable a.
The output is 4,
1 2 3 |
4 |
No Comments