Bitwise Operators in Java

·

Bitwise operators in Java are powerful tools for performing operations at the bit level. These operators are essential for low-level programming tasks such as bit manipulation, encryption, and graphics programming. This guide explores the types of bitwise operators, their functionalities, and practical examples.

Understanding Bitwise Operators

Bitwise operators work directly on the binary representations of integer types (byte, short, int, long). Each operator treats individual bits of the operands independently.

Types of Bitwise Operators

OperatorDescription
&Bitwise AND
`\`Bitwise OR
^Bitwise XOR
~Bitwise Complement (NOT)
<<Left Shift
>>Signed Right Shift
>>>Unsigned Right Shift

Detailed Breakdown of Bitwise Operators

1. Bitwise AND (&)

The Bitwise AND operator compares each bit of two numbers. If both bits are 1, the result is 1; otherwise, it's 0.

Example:

int a = 5;  // 0101
int b = 7;  // 0111
int result = a & b;  // 0101 (5)

2. Bitwise OR (\|)

The Bitwise OR operator returns 1 if at least one of the bits is 1.

Example:

int result = a | b;  // 0111 (7)

3. Bitwise XOR (^)

The Bitwise XOR operator returns 1 if the corresponding bits are different.

Example:

int result = a ^ b;  // 0010 (2)

4. Bitwise Complement (~)

This unary operator flips all bits of the operand.

Example:

int result = ~a;  // 11111010 (-6 in decimal)

5. Shift Operators

Shift operators move bits left or right, effectively multiplying or dividing by powers of two.

Left Shift (<<)

int result = a << 1;  // 1010 (10)

Right Shift (>>)

int result = a >> 1;  // 0010 (2)

Unsigned Right Shift (>>>)

int result = a >>> 1;  // 0010 (2)

Practical Applications

Example Program

public class BitwiseDemo {
    public static void main(String[] args) {
        int num1 = 4, num2 = 8;
        System.out.println("AND: " + (num1 & num2));
        System.out.println("OR: " + (num1 | num2));
        System.out.println("XOR: " + (num1 ^ num2));
        System.out.println("NOT num1: " + (~num1));
        System.out.println("Left Shift: " + (num1 << 2));
        System.out.println("Right Shift: " + (num1 >> 1));
        System.out.println("Unsigned Right Shift: " + (num1 >>> 1));
    }
}

Output:

AND: 0
OR: 12
XOR: 12
NOT num1: -5
Left Shift: 16
Right Shift: 2
Unsigned Right Shift: 2

Advantages of Bitwise Operators

👉 Explore advanced Java techniques


FAQs

Q1: When should I use bitwise operators?

A1: Use them for performance-critical tasks like cryptography, hardware interactions, or compact data storage.

Q2: Why does ~5 return -6?

A2: Java uses two's complement representation. The complement of 5 (0101) is 1010, which is -6 in decimal.

Q3: Can bitwise operators work with floating-point numbers?

A3: No, they only work with integer types (byte, short, int, long).

Q4: How does left shift multiply a number?

A4: Each left shift doubles the number (e.g., 4 << 1 is 8).

Q5: What’s the difference between >> and >>>?

A5: >> preserves the sign bit, while >>> fills shifted bits with zeros.

Q6: Are bitwise operations common in modern Java?