Operators

Java Program to Demonstrate Bitwise Operators

A Java program to demonstrate the usage of bitwise operators.

Problem Description

Write a Java program to demonstrate the usage of bitwise operators: AND (&), OR (|), XOR (^), Complement (~), Left Shift (<<), Right Shift (>>), and Unsigned Right Shift (>>>).

Code

BitwiseOperators.java
public class BitwiseOperators {
    public static void main(String[] args) {
        int a = 5;  // 0101 in binary
        int b = 3;  // 0011 in binary

        System.out.println("a & b: " + (a & b));   // 0001 = 1
        System.out.println("a | b: " + (a | b));   // 0111 = 7
        System.out.println("a ^ b: " + (a ^ b));   // 0110 = 6
        System.out.println("~a: " + (~a));         // 1010 = -6 (in 2's complement)
        System.out.println("a << 1: " + (a << 1)); // 1010 = 10
        System.out.println("a >> 1: " + (a >> 1)); // 0010 = 2
        System.out.println("a >>> 1: " + (a >>> 1)); // 0010 = 2
    }
}

Output

a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
a >>> 1: 2

Explanation

  1. & (Bitwise AND): Performs AND on each bit.
  2. | (Bitwise OR): Performs OR on each bit.
  3. ^ (Bitwise XOR): Performs XOR on each bit.
  4. ~ (Bitwise Complement): Inverts all bits.
  5. << (Left Shift): Shifts bits to the left, filling with 0.
  6. >> (Right Shift): Shifts bits to the right, preserving sign.
  7. >>> (Unsigned Right Shift): Shifts bits to the right, filling with 0.

Try Yourself

  1. Odd or Even: Use & to check if a number is odd or even. (Hint: n & 1).
  2. Swap with XOR: Swap two numbers using only ^ and no temporary variable.
  3. Negative Shift: Try shifting a negative number (e.g., -5) using >> and >>>. What is the difference?
  4. Power of 2: Multiply a number by 2 using <<. Divide by 2 using >>.
  5. Brain Twister: How can you check if a number is a power of 2 using bitwise operators? Hint: (n & (n - 1)) == 0