712660. Bit's basic operations

Medium

Bit's Basic Operations

Slug: bits-basic-operations

Difficulty: Medium

Id: 712660

Topic Tags: None

Company Tags: None

Summary

Bit-level operations are fundamental in computer programming, and understanding these basic operations is crucial for any aspiring programmer. This problem involves performing various bit-level operations such as AND, OR, XOR, left shift, right shift, and flipping bits.

Detailed Explanation

The given task requires you to perform different bit-level operations on a number, which represents a binary code. You need to write an algorithm that can efficiently apply these operations.

Here are the steps for each operation:

  1. AND: This operation compares each bit of the first operand (number) to the corresponding bit of the second operand. If both bits are 1, then the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

    • Example: Input number = 5 (101), and input number2 = 3 (011). The output will be 1 (001).
  2. OR: This operation compares each bit of the first operand (number) to the corresponding bit of the second operand. If either bit is 1, then the corresponding result bit is set to 1.

    • Example: Input number = 5 (101), and input number2 = 3 (011). The output will be 7 (111).
  3. XOR: This operation compares each bit of the first operand (number) to the corresponding bit of the second operand. If the bits are not the same, then the corresponding result bit is set to 1.

    • Example: Input number = 5 (101), and input number2 = 3 (011). The output will be 6 (110).
  4. Left Shift: This operation shifts the bits of the first operand (number) to the left by a specified number of positions, filling the vacated spaces with zeros.

    • Example: Input number = 5 (101), and shift position = 1. The output will be 10 (1010).
  5. Right Shift: This operation shifts the bits of the first operand (number) to the right by a specified number of positions, filling the vacated spaces with zeros.

    • Example: Input number = 10 (1010), and shift position = 1. The output will be 5 (101).
  6. Flip: This operation flips each bit of the given number, i.e., it changes all the 0s to 1s and all the 1s to 0s.

    • Example: Input number = 5 (101). The output will be 2 (010).

Optimized Solutions

Java

View on GeeksforGeeks
public class BitOperations {
    public static void main(String[] args) {
        int num = 5; // Binary representation of 5 is 101
        int num2 = 3; // Binary representation of 3 is 011
        
        // AND operation
        System.out.println("AND: " + ((num & num2)));
        
        // OR operation
        System.out.println("OR: " + (num | num2));
        
        // XOR operation
        System.out.println("XOR: " + (num ^ num2));
        
        // Left shift operation
        System.out.println("Left Shift: " + (num << 1));
        
        // Right shift operation
        System.out.println("Right Shift: " + (num >> 1));
        
        // Flip bits
        System.out.println("Flip Bits: " + (~num));
    }
}

DevExCode

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

© 2026 DevExCode. All rights reserved.