Bit-Manipulation-Interview-Questions with embeddedprep.com
Bit-Manipulation-Interview-Questions with embeddedprep.com

Bit Manipulation Interview Questions

Bit manipulation is a fundamental concept in computer science that involves direct operations on individual bits of a number. It is widely used in low-level programming, embedded systems, competitive programming, and optimization techniques.

What is Bit Manipulation?

Bit manipulation refers to operations that modify bits directly using bitwise operators. These operations enable efficient computation, reducing memory usage and increasing processing speed.

Why Use Bit Manipulation?

  • Faster computations than arithmetic operations.
  • Space optimization (useful in embedded systems).
  • Direct hardware interaction.
  • Useful in cryptography, compression, and networking.

Bitwise Operators in C and C++:

OperatorSymbolDescriptionExample (a = 5 (0101), b = 3 (0011))Output
AND&Sets bits to 1 if both are 1, otherwise 0a & b0101 & 00110001 (1)
OR``Sets bits to 1 if at least one is 1`a
XOR^Sets bits to 1 if they are differenta ^ b0101 ^ 00110110 (6)
NOT~Inverts all bits (1 → 0, 0 → 1)~a~01011111...1010 (-6 in 2’s complement)
Left Shift<<Shifts bits left by n places (multiplies by 2^n)a << 10101 << 11010 (10)
Right Shift>>Shifts bits right by n places (divides by 2^n)a >> 10101 >> 10010 (2)

Applications of Bit Manipulation

  • Efficient Data Storage: Compact storage of flags using bit fields.
  • Cryptography: XOR encryption algorithms.
  • Networking: IP masking, subnetting.
  • Image Processing: Pixel bit operations.
  • Game Development: Bitwise operations for collision detection.

Basic Bit Manipulation Interview Questions

1. What are Bitwise Operators in C/C++?

Answer:
Bitwise operators perform operations at the bit level. The main bitwise operators in C/C++ are:

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT)
  • << (Left Shift)
  • >> (Right Shift)

Example:

int a = 5, b = 3;
cout << (a & b); // Output: 1 (0101 & 0011 = 0001)
cout << (a | b); // Output: 7 (0101 | 0011 = 0111)

2. How do you check if a number is even or odd using bit manipulation?

Answer:
Use the AND operator with 1. If the least significant bit (LSB) is 1, the number is odd; otherwise, it’s even.

bool isOdd(int n) {
    return (n & 1);
}

Example:

cout << isOdd(5); // Output: 1 (true)
cout << isOdd(4); // Output: 0 (false)

3. How do you check if a number is a power of 2?

Answer:
A number is a power of 2 if it has only one set bit. (n & (n - 1)) removes the lowest set bit. If the result is 0, it’s a power of 2.

bool isPowerOfTwo(int n) {
    return (n > 0) && ((n & (n - 1)) == 0);
}

Example:

cout << isPowerOfTwo(8); // Output: 1 (true)
cout << isPowerOfTwo(10); // Output: 0 (false)

4. How do you count the number of set bits in an integer?

Answer:
Using Brian Kernighan’s Algorithm, which turns off the rightmost set bit in each iteration.

int countSetBits(int n) {
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

Example:

cout << countSetBits(5); // Output: 2 (101 has two 1s)

5. How do you find the XOR of all numbers from 1 to n?

Answer:
There is a pattern:

  • n % 4 == 0XOR = n
  • n % 4 == 1XOR = 1
  • n % 4 == 2XOR = n + 1
  • n % 4 == 3XOR = 0
int xorFrom1ToN(int n) {
    if (n % 4 == 0) return n;
    if (n % 4 == 1) return 1;
    if (n % 4 == 2) return n + 1;
    return 0;
}

6. How do you find the only non-repeating element in an array where every other element appears twice?

Answer:
Use XOR, as a ^ a = 0 and 0 ^ b = b.

int findUnique(int arr[], int n) {
    int result = 0;
    for (int i = 0; i < n; i++) {
        result ^= arr[i];
    }
    return result;
}

Example:

int arr[] = {2, 3, 5, 3, 2};
cout << findUnique(arr, 5); // Output: 5

7. How do you swap two numbers without using a temporary variable?

Answer:
Use XOR swapping:

void swap(int &a, int &b) {
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}

Example:

int x = 3, y = 4;
swap(x, y);
cout << x << " " << y; // Output: 4 3

8. How do you reverse the bits of an integer?

Answer:

unsigned int reverseBits(unsigned int n) {
    unsigned int rev = 0;
    for (int i = 0; i < 32; i++) {
        rev = (rev << 1) | (n & 1);
        n >>= 1;
    }
    return rev;
}

9. How do you find the position of the rightmost set bit?

Answer:
Using n & -n gives the rightmost set bit. To get its position, use log2().

int rightmostSetBitPosition(int n) {
    return log2(n & -n) + 1;
}

Example:

cout << rightmostSetBitPosition(18); // Output: 2 (18 = 10010, rightmost set bit is at position 2)

10. How do you toggle the kth bit of a number?

Answer:
Use the XOR operation with (1 << k).

int toggleKthBit(int n, int k) {
    return n ^ (1 << k);
}

Example:

cout << toggleKthBit(5, 1); // Output: 7 (0101 -> 0111)

11. How do you turn off the kth bit of a number?

Answer:
Use & with the negation of (1 << k).

int turnOffKthBit(int n, int k) {
    return n & ~(1 << k);
}

12. How do you check if two numbers have opposite signs?

Answer:
Use the XOR operator. If x ^ y < 0, they have opposite signs.

bool oppositeSigns(int x, int y) {
    return (x ^ y) < 0;
}

13. How do you find the two non-repeating elements in an array where every other element appears twice?

Answer:
Use XOR to find x ^ y, then use the rightmost set bit to separate numbers into two groups.

void findTwoUnique(int arr[], int n) {
    int xorAll = 0;
    for (int i = 0; i < n; i++) xorAll ^= arr[i];

    int setBit = xorAll & -xorAll;
    int x = 0, y = 0;
    
    for (int i = 0; i < n; i++) {
        if (arr[i] & setBit) x ^= arr[i];
        else y ^= arr[i];
    }
    
    cout << x << " " << y;
}

14. How do you multiply a number by 2 using bitwise operations?

Answer:
Left shift << 1:

int multiplyByTwo(int n) {
    return n << 1;
}

15. How do you divide a number by 2 using bitwise operations?

Answer:
Right shift >> 1:

int divideByTwo(int n) {
    return n >> 1;
}

Bit Manipulation Interview Questions

1. How do you set the 3rd bit of a number to 1?

Answer:
Use the OR (|) operator with (1 << k).

int num = 5; // 0101
num = num | (1 << 3); // Set 3rd bit
cout << num; // Output: 13 (1101)

2. How do you clear the 2nd bit of a number?

Answer:
Use AND (&) with the negation of (1 << k).

int num = 7; // 0111
num = num & ~(1 << 2); // Clear 2nd bit
cout << num; // Output: 3 (0011)

3. How do you toggle the 1st and 3rd bits of a number?

Answer:
Use XOR (^) with (1 << k).

int num = 5; // 0101
num = num ^ (1 << 1); // Toggle 1st bit
num = num ^ (1 << 3); // Toggle 3rd bit
cout << num; // Output: 13 (1101)

4. How do you check if the 4th bit of a number is set or not?

Answer:
Use AND (&) with (1 << k).

int num = 9; // 1001
bool isSet = (num & (1 << 3)) != 0; // Check if 4th bit is 1
cout << isSet; // Output: 1 (true)

5. How do you turn off the rightmost set bit of a number?

Answer:
Use n & (n - 1).

int num = 6; // 0110
num = num & (num - 1); // Removes rightmost 1
cout << num; // Output: 4 (0100)

6. How do you check if a number has an odd or even number of 1’s?

Answer:
Use XOR reduction to find parity.

bool hasOddSetBits(int n) {
    bool parity = 0;
    while (n) {
        parity ^= (n & 1);
        n >>= 1;
    }
    return parity;
}

Answer:
Use n | (n + 1).

int num = 10; // 1010
num = num | (num + 1);
cout << num; // Output: 11 (1011)

8. How do you get the bitwise complement of a number (invert all bits)?

Answer:
Use ~n.

int num = 5; // 0101
cout << ~num; // Output: -6 (Two’s complement representation)

9. How do you find the position of the most significant set bit (highest bit set)?

Answer:
Use log2(n) + 1.

int highestBitPosition(int n) {
    return log2(n) + 1;
}

Example:

cout << highestBitPosition(18); // Output: 5 (18 = 10010)

10. How do you set all bits after the lowest set bit?

Answer:
Use n | (n - 1).

int num = 18; // 10010
num = num | (num - 1);
cout << num; // Output: 19 (10011)

11. How do you clear all bits after the lowest set bit?

Answer:
Use n & -n.

int num = 18; // 10010
num = num & -num;
cout << num; // Output: 2 (00010)

12. How do you check if all bits in a number are set?

Answer:
Compare with (1 << n) - 1.

bool allBitsSet(int n, int numBits) {
    return n == (1 << numBits) - 1;
}

Example:

cout << allBitsSet(15, 4); // Output: 1 (true, 1111)

13. How do you toggle all bits up to the kth bit?

Answer:
Use n ^ ((1 << k) - 1).

int num = 9; // 1001
num = num ^ ((1 << 3) - 1);
cout << num; // Output: 6 (0110)

14. How do you find the number of bits needed to flip to convert a to b?

Answer:
Use countSetBits(a ^ b).

int countSetBits(int n) {
    int count = 0;
    while (n) {
        n &= (n - 1);
        count++;
    }
    return count;
}
int bitFlips(int a, int b) {
    return countSetBits(a ^ b);
}

Example:

cout << bitFlips(10, 20); // Output: 4

15. How do you reverse only the lowest k bits of a number?

Answer:
Use n ^ ((1 << k) - 1).

int reverseLowestKBits(int n, int k) {
    return n ^ ((1 << k) - 1);
}

Bit Manipulation Tricks

1. Check if a number is even or odd

Trick: n & 1 → If the last bit is 1, the number is odd; if 0, it’s even.

bool isOdd(int n) {
    return (n & 1);
}

🔹 Example: isOdd(5) → true (odd) | isOdd(8) → false (even)

2. Swap two numbers without a temp variable

Trick: Use XOR a ^= b; b ^= a; a ^= b;

void swap(int &a, int &b) {
    a ^= b;
    b ^= a;
    a ^= b;
}

🔹 Example: (a, b) = (5, 3) → (3, 5)

3. Check if a number is a power of 2

Trick: n & (n - 1) == 0 (only powers of 2 have a single 1 bit)

bool isPowerOfTwo(int n) {
    return (n > 0) && ((n & (n - 1)) == 0);
}

🔹 Example: isPowerOfTwo(8) → true | isPowerOfTwo(7) → false

4. Count set bits in a number

Trick: Use Brian Kernighan’s Algorithm (reduces runtime to O(log n))

int countSetBits(int n) {
    int count = 0;
    while (n) {
        n &= (n - 1);
        count++;
    }
    return count;
}

🔹 Example: countSetBits(7) → 3 (0111)

5. Find the position of the rightmost set bit

Trick: n & -n gives only the lowest set bit

int getRightmostSetBitPos(int n) {
    return log2(n & -n) + 1;
}

🔹 Example: getRightmostSetBitPos(18) → 2 (10010)

6. Turn off the rightmost set bit

Trick: n & (n - 1)

int turnOffRightmostSetBit(int n) {
    return n & (n - 1);
}

🔹 Example: turnOffRightmostSetBit(6) → 4 (0110 → 0100)

7. Toggle all bits up to the k-th position

Trick: n ^ ((1 << k) - 1)

int toggleLowestKBits(int n, int k) {
    return n ^ ((1 << k) - 1);
}

🔹 Example: toggleLowestKBits(9, 3) → 6 (1001 → 0110)

8. Find XOR of all numbers from 1 to n

Trick: XOR(1 to n) = { n, 1, n+1, 0 } [based on n % 4]

int xorUptoN(int n) {
    if (n % 4 == 0) return n;
    if (n % 4 == 1) return 1;
    if (n % 4 == 2) return n + 1;
    return 0;
}

🔹 Example: xorUptoN(5) → 1

9. Check if two numbers have opposite signs

Trick: x ^ y < 0 (MSB differs → signs are different)

bool hasOppositeSigns(int x, int y) {
    return (x ^ y) < 0;
}

🔹 Example: hasOppositeSigns(5, -3) → true

10. Find the only non-repeating element in an array where every other element appears twice

Trick: XOR all elements → Only unique element remains

int findUnique(vector<int>& nums) {
    int result = 0;
    for (int num : nums) {
        result ^= num;
    }
    return result;
}

🔹 Example: findUnique({1, 2, 3, 2, 1}) → 3

Frequently Asked Questions (FAQ) on Bit Manipulation:

1. What is bit manipulation?

Bit manipulation is the process of directly operating on individual bits of binary numbers using bitwise operators like AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<), and Right Shift (>>).

2. Why is bit manipulation important in programming?

Bit manipulation optimizes performance by reducing execution time and memory usage. It is widely used in embedded systems, cryptography, networking, compression algorithms, and competitive programming.

3. What are the common bitwise operators in C/C++?

The common bitwise operators are:

  • AND (&) – Sets bits to 1 only if both bits are 1.
  • OR (|) – Sets bits to 1 if at least one bit is 1.
  • XOR (^) – Sets bits to 1 if the bits differ.
  • NOT (~) – Inverts all bits (1 → 0, 0 → 1).
  • Left Shift (<<) – Shifts bits left (multiplies by 2^n).
  • Right Shift (>>) – Shifts bits right (divides by 2^n).

4. How can you check if a number is even or odd using bit manipulation?

Use n & 1:

  • If n & 1 == 0, the number is even.
  • If n & 1 == 1, the number is odd.
bool isOdd(int n) {
    return (n & 1);
}

5. How do you swap two numbers without using a temporary variable?

Using XOR (^):

void swap(int &a, int &b) {
    a ^= b;
    b ^= a;
    a ^= b;
}

6. How do you check if a number is a power of 2?

A number is a power of 2 if it has exactly one 1 bit in binary.

bool isPowerOfTwo(int n) {
    return (n > 0) && ((n & (n - 1)) == 0);
}

7. How can you count the number of 1s (set bits) in a number?

Use Brian Kernighan’s Algorithm:

int countSetBits(int n) {
    int count = 0;
    while (n) {
        n &= (n - 1); // Removes the rightmost set bit
        count++;
    }
    return count;
}

8. How do you find the rightmost set bit of a number?

Use n & -n:

int getRightmostSetBit(int n) {
    return n & -n;
}

🔹 Example: getRightmostSetBit(18) → 2 (10010)

9. How do you toggle (flip) a specific bit in a number?

Use XOR with a mask:

int toggleBit(int n, int k) {
    return n ^ (1 << k);
}

🔹 Example: toggleBit(5, 1) → 7 (0101 → 0111)

10. How do you find the unique number in an array where every other element appears twice?

Use XOR on all elements:

int findUnique(vector<int>& nums) {
    int result = 0;
    for (int num : nums) {
        result ^= num;
    }
    return result;
}

🔹 Example: findUnique({1, 2, 3, 2, 1}) → 3

Real-Time Scenario-Based Interview Questions

After Reading above tutorials if you want to deep dive into concept then you can take a time and think about below bit Manipulation Real-Time Scenario-Based Interview Questions

1. Basic Bitwise Operations

  • How do you check if a given number is even or odd using bitwise operations?
  • Can you swap two numbers without using a temporary variable?
  • How do you check if the k-th bit is set (1) or not (0) in a number?
  • Write a function to set, clear, toggle, and test a bit in a given integer.

2. Bitwise Tricks for Optimization

  • How do you find the only non-repeating element in an array where every other element appears twice?
  • How can you efficiently multiply or divide a number by powers of 2 using bitwise operations?
  • Write a function to toggle the lowest k bits of a given number.
  • How do you detect if two numbers have opposite signs using bitwise operations?

3. Power of Two & Bit Counting

  • How can you check if a number is a power of two using bit manipulation?
  • Implement a function to count the number of 1s (set bits) in a binary representation of a number.
  • Write an efficient algorithm to find the position of the rightmost set bit in a number.
  • How do you turn off the rightmost set bit in a given integer?

4. Advanced Bitwise Problems

  • How do you efficiently compute XOR from 1 to N?
  • How do you find the only two non-repeating numbers in an array where every other number appears twice?
  • How do you reverse the bits of a given 32-bit integer?
  • Write a function to find the missing number in an array containing numbers from 0 to N, given that only one number is missing.

5. Real-Time Embedded System Scenarios

  • In an embedded system, how would you use bitwise operations to control hardware registers?
  • How can you use bit manipulation to store multiple flags efficiently in a single variable?
  • If a sensor provides 8-bit data, but your system processes 16-bit values, how would you efficiently extract and manipulate the required bits?
  • How do you implement a bitmask-based permission system using bitwise operations?

6. Bitwise Manipulation in Image Processing & Networking

  • How can you extract the red, green, and blue (RGB) components from a 24-bit color code?
  • How would you use bitwise operations for efficient checksum calculations in network packets?
  • In a microcontroller project, how do you implement a circular buffer using bitwise operations?

Basic Bit Manipulation MCQs

1. What is the result of 5 & 3 in binary operations?

  • A) 7
  • B) 3
  • C) 1
  • D) 5
    ✅ Answer: C) 1
    Explanation: 5 (101) & 3 (011) = 001 (1 in decimal)

2. What does the expression (x | (1 << n)) do?

  • A) Clears the nth bit of x
  • B) Sets the nth bit of x
  • C) Toggles the nth bit of x
  • D) Checks if the nth bit is set
    ✅ Answer: B) Sets the nth bit of x
    Explanation: The left shift moves 1 to the nth position and OR (|) ensures the bit is set.

3. What will 10 >> 1 (right shift) evaluate to?

  • A) 5
  • B) 20
  • C) 2
  • D) 8
    ✅ Answer: A) 5
    Explanation: 10 (1010) >> 1 shifts bits right by 1, resulting in 0101 (5 in decimal).

4. How do you check if the nth bit of x is set?

  • A) x & (1 << n)
  • B) x | (1 << n)
  • C) x ^ (1 << n)
  • D) x ~ (1 << n)
    ✅ Answer: A) x & (1 << n)
    Explanation: ANDing with (1 << n) checks if the nth bit is set (1).

5. What does x ^ (1 << n) do?

  • A) Clears the nth bit
  • B) Sets the nth bit
  • C) Toggles the nth bit
  • D) Checks if the nth bit is set
    ✅ Answer: C) Toggles the nth bit
    Explanation: XOR flips the bit—1 becomes 0, and 0 becomes 1.

Intermediate Bit Manipulation MCQs

6. Which bitwise operator is used to clear a particular bit?

  • A) AND (&)
  • B) OR (|)
  • C) XOR (^)
  • D) NOT (~)
    ✅ Answer: A) AND (&)
    Explanation: x & ~(1 << n) clears the nth bit.

7. What is the result of 12 | 5?

  • A) 5
  • B) 12
  • C) 13
  • D) 15
    ✅ Answer: D) 13
    Explanation: 12 (1100) | 5 (0101) = 1101 (13 in decimal)

8. What is the number of set bits in 29 (binary 11101)?

  • A) 2
  • B) 3
  • C) 4
  • D) 5
    ✅ Answer: D) 5
    Explanation: 29 (11101) has five set bits.

9. How do you efficiently count set bits in an integer?

  • A) Using a loop and checking bits one by one
  • B) Using n & (n-1) repeatedly
  • C) Using x | (1 << n)
  • D) None of the above
    ✅ Answer: B) Using n & (n-1) repeatedly
    Explanation: n & (n-1) removes the rightmost set bit in O(number_of_1s) time.

10. What does x & (-x) return?

  • A) The most significant set bit
  • B) The least significant set bit
  • C) Clears all bits
  • D) None of the above
    ✅ Answer: B) The least significant set bit
    Explanation: -x is 2's complement of x, and x & (-x) isolates the rightmost 1.

11. How do you check if a number is a power of 2?

  • A) x & (x - 1) == 0
  • B) x | (x - 1) == 0
  • C) x ^ (x - 1) == 0
  • D) None of the above
    ✅ Answer: A) x & (x - 1) == 0
    Explanation: Powers of 2 have only one set bit, so x & (x-1) == 0 works.

12. What is the result of ~5 in a 32-bit system?

  • A) -6
  • B) 5
  • C) -5
  • D) 6
    ✅ Answer: A) -6
    Explanation: ~5 inverts all bits, leading to -(5+1) = -6 (Two’s complement representation).

13. What does x << 3 do?

  • A) Multiply x by 2
  • B) Multiply x by 8
  • C) Divide x by 2
  • D) Divide x by 8
    ✅ Answer: B) Multiply x by 8
    Explanation: Left shift by n is equivalent to multiplying by 2^n.

14. Which operator is used for swapping two numbers using bitwise operations?

  • A) |
  • B) ^
  • C) &
  • D) <<
    ✅ Answer: B) ^ (XOR)
    Explanation: Swapping using XOR: x = x ^ y; y = x ^ y; x = x ^ y;

15. How do you reverse bits in an integer most efficiently?

  • A) Using a loop and bitwise operations
  • B) Using lookup tables
  • C) Using recursion
  • D) None of the above
    ✅ Answer: B) Using lookup tables
    Explanation: Lookup tables provide an O(1) method for reversing bits.

Thank you for exploring this tutorial! Stay ahead in embedded systems with expert insights, hands-on projects, and in-depth guides. Follow Embedded Prep for the latest trends, best practices, and step-by-step tutorials to enhance your expertise. Keep learning, keep innovating!

You can also Visit .

Spread the knowledge with embedded prep
Show 4 Comments

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *