4 Bitwise Operators in Java: A Comprehensive Guide with Examples
Bitwise Operators in Java
Bitwise operators in Java are used to perform operations at the bit level. Since computers store numbers in binary format, bitwise operations allow for efficient manipulation of individual bits within an integer. These operators are particularly useful in areas like low-level programming, cryptography, and optimization of arithmetic operations.
Types of Bitwise Operators in Java
Java provides the following bitwise operators:
- AND (&) Operator (Performs Bitwise AND operation)
- OR (|) Operator (Performs Bitwise OR operation)
- XOR (^) Operator (Performs Bitwise XOR operation)
- Complement (~) Operator (Inverts all bits – one’s complement)
Let’s explore these operators with practical examples.
Bitwise AND (&)
The AND operator performs a bitwise AND operation between corresponding bits of two numbers. A bit is set to 1
only if both corresponding bits are 1
.
Example:
public class BitwiseAND { public static void main(String[] args) { int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a & b; // Binary: 0001 (Decimal: 1) System.out.println("5 & 3 = " + result); } }
Output:
5 & 3 = 1
Bitwise OR (|)
The OR operator performs a bitwise OR operation between corresponding bits. A bit is set to 1
if at least one of the corresponding bits is 1
.
Example:
public class BitwiseOR { public static void main(String[] args) { int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a | b; // Binary: 0111 (Decimal: 7) System.out.println("5 | 3 = " + result); } }
Output:
5 | 3 = 7
Bitwise XOR (^)
The XOR operator returns 1
if the corresponding bits of operands are different; otherwise, it returns 0
.
Example:
public class BitwiseXOR { public static void main(String[] args) { int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a ^ b; // Binary: 0110 (Decimal: 6) System.out.println("5 ^ 3 = " + result); } }
Output:
5 ^ 3 = 6
Bitwise Complement (~)
The complement operator inverts all bits of a number (flips 1
to 0
and vice versa). Since Java uses two’s complement representation for negative numbers, ~x = -(x + 1)
.
Example:
public class BitwiseComplement { public static void main(String[] args) { int a = 5; // Binary: 0101 int result = ~a; // Binary: 1010 (Decimal: -6 in two’s complement) System.out.println("~5 = " + result); } }
Output:
~5 = -6
Additional Bitwise Tricks and Applications
Checking Even or Odd Using Bitwise AND
You can check whether a number is even or odd using the bitwise AND operator.
public class CheckEvenOdd { public static void main(String[] args) { int num = 7; if ((num & 1) == 0) { System.out.println(num + " is Even"); } else { System.out.println(num + " is Odd"); } } }
Output:
7 is Odd
Swapping Two Numbers Using XOR
XOR can be used to swap two numbers without using a temporary variable.
public class SwapUsingXOR { public static void main(String[] args) { int a = 5, b = 3; a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("After swap: a = " + a + ", b = " + b); } }
Output:
After swap: a = 3, b = 5
Checking if a Number is a Power of Two
A number is a power of two if it has only one 1
bit in its binary representation. This can be checked using n & (n - 1) == 0
.
public class PowerOfTwoCheck { public static boolean isPowerOfTwo(int n) { return (n > 0) && ((n & (n - 1)) == 0); } public static void main(String[] args) { int num = 16; System.out.println(num + " is power of two: " + isPowerOfTwo(num)); } }
Output:
16 is power of two: true
Counting Set Bits in an Integer
This method counts the number of 1
bits (Hamming weight) in an integer.
public class CountSetBits { public static int countBits(int n) { int count = 0; while (n > 0) { count += (n & 1); n >>= 1; } return count; } public static void main(String[] args) { int num = 9; // Binary: 1001 System.out.println("Number of set bits in " + num + " = " + countBits(num)); } }
Output:
Number of set bits in 9 = 2
Conclusion
Bitwise operators in Java provide powerful tools for manipulating binary data efficiently. Understanding these operators is crucial for working with low-level optimizations, cryptography, and memory-efficient algorithms. By practicing with different numbers and operators, you can improve your grasp of bitwise operations and leverage them in various programming scenarios.
If you like this article, please comment, share and subscribe. For more such articles, visit my blog.
About the author : Mohit Jain
I have more than five years of work experience as a Java Developer. I am passionate about teaching and learning new technologies. I specialize in Java 8, Hibernate, Spring Framework, Spring Boot, and databases like MySQL, and Oracle. I like to share my knowledge with others in the form of articles.