878743. Bitwise Operators

Basic

Bitwise Operators

Summary

The problem involves using bitwise operators to manipulate binary numbers. The key concept is understanding how bitwise operations work and applying them to solve a specific problem.

Detailed Explanation

Bitwise operators are used to perform operations on each bit of a binary number. They are useful when dealing with binary data or performing bitwise operations in programming.

Here's a step-by-step breakdown of the solution:

  1. Initialize two variables a and b as input integers.
  2. Use bitwise AND (&) to find the bits that are set in both a and b. This operation returns a value where each bit is set if the corresponding bits in a and b are set.
  3. Use bitwise OR (|) to find the bits that are set in either a or b. This operation returns a value where each bit is set if the corresponding bits in a or b are set.
  4. Use bitwise XOR (^) to find the bits that are different in a and b. This operation returns a value where each bit is set if the corresponding bits in a and b are different.

The time complexity of this solution is O(1) as we're performing constant-time operations. The space complexity is also O(1) as we're not using any extra space.

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static void main(String[] args) {
        int a = 5; // 00000101
        int b = 3; // 00000011

        System.out.println("Bitwise AND: " + (a & b)); // 00000001
        System.out.println("Bitwise OR: " + (a | b)); // 00000111
        System.out.println("Bitwise XOR: " + (a ^ b)); // 00000110
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.