Introduction to Bitwise Operators in Java
In Java, Bitwise operators are binary operators that works on bits to perform its operations. In other words, Java's bitwise operators perform Bitwise OR, Bitwise AND, Bitwise XOR, and Bitwise Complement. Bitwise operators in java, can be applied to the integer types, long, int, short, char, and byte.
Java supports the following Bitwise operators.
- Bitwise OR Operator(|)
- Bitwise AND Operator(&)
- Bitwise XOR Operator (^)
- Bitwise Complement Operator (~)
The generalized syntax to use all the bitwise operator is as shown below:
left_operand bitwise_operator right_operand
Different types of Bitwise operators in Java
1. Bitwise OR Operator (|)
In Java, bitwise OR operator "|" is a binary operator that operates on the bits. This operator returns 1 if either of the bit is 1. However, if both the bits are 0, it returns 0. Moreover, when we use it with char type, it operates on the ASCII value of that character.
The statement below demonstrates the "|" operator with the operands.
result = left_operand | right_operand
Example : This example demonstrates the working of | operator with binary numbers.
8 | 2 1000 | 0010 ______ 1010 (10 in decimal)
public class BitwiseDemo
{
public static void main(String[] args)
{
//Initializing
int a=8,b=2;
char c='A';
//Using bitwise OR with int
System.out.println("Binary equivalent of 8 is 1000 ");
System.out.println("Binary equivalent of 2 is 0010 ");
System.out.println("Bitwise OR of 8 and 2 is "+(a|b)+" in decimal");
//Using bitwise OR with int and char
System.out.println("\nBinary equivalent of A is 01000001 ");
System.out.println("Binary equivalent of 2 is 0010 ");
System.out.println("Bitwise OR of A and 2 is "+(c|b)+" in decimal");
}
}
Output
Binary equivalent of 8 is 1000
Binary equivalent of 2 is 0010
Bitwise OR of 8 and 2 is 10 in decimal
Binary equivalent of A is 01000001
Binary equivalent of 2 is 0010
Bitwise OR of A and 2 is 67 in decimal
2. Bitwise AND Operator (&)
In Java, bitwise AND operator "&" is a binary operator that operates on the bits. This operator returns 1 if both the bits are 1. However, if either of the bit is 0, it returns 0. Moreover, when we use it with char type, it operates on the ASCII value of that character.
The statement below demonstrates the "&" operator with the operands.
result = left_operand & right_operand
Example : This example demonstrates the working of & operator with binary numbers.
7 & 3 0111 & 0011 _______ 0011 (3 in decimal)
public class BitwiseDemo
{
public static void main(String[] args)
{
//Initializing
int a=7,b=3;
char c='A';
//Using bitwise AND with int
System.out.println("Binary equivalent of 7 is 0111 ");
System.out.println("Binary equivalent of 3 is 0011 ");
System.out.println("Bitwise AND of 7 and 3 is "+(a&b)+" in decimal");
//Using bitwise AND with int and char
System.out.println("\nBinary equivalent of A is 01000001 ");
System.out.println("Binary equivalent of 3 is 0011 ");
System.out.println("Bitwise AND of A and 3 is "+(c&b)+" in decimal");
}
}
Output
Binary equivalent of 7 is 0111
Binary equivalent of 3 is 0011
Bitwise AND of 7 and 3 is 3 in decimal
Binary equivalent of A is 01000001
Binary equivalent of 3 is 0011
Bitwise AND of A and 3 is 1 in decimal
3. Bitwise XOR Operator (^)
In Java, bitwise XOR operator "^" is a binary operator that operates on the bits. This operator returns 1 if both the bits are different. However, if both the bits are same, it returns 0. Moreover, when we use it with char type, it operates on the ASCII value of that character.
The statement below demonstrates the "^" operator with the operands.
result = left_operand ^ right_operand
Example : This example demonstrates the working of ^ operator with binary numbers.
7 ^ 3 0111 ^ 0011 _______ 0100 (4 in decimal)
public class BitwiseDemo
{
public static void main(String[] args)
{
//Initializing
byte a=7,b=3;
char c='A';
//Using bitwise XOR with byte
System.out.println("Binary equivalent of 7 is 0111 ");
System.out.println("Binary equivalent of 3 is 0011 ");
System.out.println("Bitwise XOR of 7 and 3 is "+(a^b)+" in decimal");
//Using bitwise XOR with byte and char
System.out.println("\nBinary equivalent of A is 01000001 ");
System.out.println("Binary equivalent of 3 is 0011 ");
System.out.println("Bitwise XOR of A and 3 is "+(c^b)+" in decimal");
}
}
Output
Binary equivalent of 7 is 0111
Binary equivalent of 3 is 0011
Bitwise XOR of 7 and 3 is 4 in decimal
Binary equivalent of A is 01000001
Binary equivalent of 3 is 0011
Bitwise XOR of A and 3 is 66 in decimal
4. Bitwise Complement Operator (~)
In Java, bitwise Complement operator "~" is a unary operator that operates on the bits. This operator returns the inverse or complement of the bit. In other words, it makes every 0 a 1 and every 1 a 0. Moreover, when we use it with char type, it operates on the ASCII value of that character. Here, it also flips the sign of a number. So, the complement of positive number will be always negative. We cannot directly convert the result into decimal and get the desired output.
The statement below demonstrates the "~" operator with the operands.
result = ~ operand
Example : This example demonstrates the working of ~ operator with binary numbers.
~9 ~ 1001 _______ 0110 (-10)
public class BitwiseDemo
{
public static void main(String[] args)
{
//Initializing
byte a=9;
char c='A';
//Using bitwise complement with byte
System.out.println("Binary equivalent of 9 is 1001 ");
System.out.println("Bitwise Complement of 9 is "+(~a));
//Using bitwise complement with char
System.out.println("\nBinary equivalent of A is 01000001 ");
System.out.println("Bitwise Complement of A is "+(~c));
}
}
Output
Binary equivalent of 9 is 1001
Bitwise Complement of 9 is -10
Binary equivalent of A is 01000001
Bitwise Complement of A is -66
Summary
The knowledge of bitwise operators is a key to start building the logic in Java. The Bitwise operators are extensively used when application works with bits of data. In this tutorial, we covered all bitwise operators supported in Java. We learned in detail about the syntax and how we can use this operators with different data types with example. All in all, this tutorial, covers everything that you need to know in order to have a solid command over bitwise operators in Java.
Further Reading
Java Operators Explained [Easy Examples]
References
Operators
More about Java Operators