Top MCQs on Bitwise Algorithms and Bit Manipulations with Answers

The Bitwise Algorithms is used to perform operations at the bit-level or to manipulate bits in different ways. The bitwise operations are found to be much faster and are sometimes used to improve the efficiency of a program. More on Bitwise Algorithm
Bit manipulation Quiz

Bit manipulation Quiz

Question 1

What is the return value of following function for arr[] = {9, 12, 2, 11, 2, 2, 10, 9, 12, 10, 9, 11, 2} and n is size of this array. 

C

int fun(int arr[], int n)
{
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}
Cross

0

Tick

9

Cross

12

Cross

2



Question 1-Explanation: 

Note that 9 is the only element with odd occurrences, all other elements have even occurrences. If the input array has all elements with even occurrences except one, then the function returns the only element with odd occurrences. Note that XORing an element with itself results in 0 and XOR of 0 with a number x is equal to x. Try following the complete program.

# include <iostream>
using namespace std;

int fun(int arr[], int n)
{
    int x = arr[0];
    for (int i = 1; i < n; i++)
        x = x ^ arr[i];
    return x;
}

int main()
{
  int arr[] = {9, 12, 2, 11, 10, 9, 12, 10, 9, 11, 2};
  int n = sizeof(arr)/sizeof(arr[0]);
  cout << fun(arr, n) << endl;
  return 0;
}

Hence option (B) is the correct answer.

Question 2

What does the following C expression do? x = (x<<1) + x + (x>>1);

Cross

Multiplies an integer with 7

Tick

Multiplies an integer with 3.5

Cross

Multiplies an integer with 3

Cross

Multiplies an integer with 8



Question 2-Explanation: 

In the expression: x = (x<<1) + x + (x>>1);
(x<< 1) means left shift x by 1, by doing this we get, x*2.
(x>>1) means right shift x by 1, by doing this we get, x/2

After solving the above expression, we get
=x*2 + x + x/2 
= 3*x +x/2
=7x/2
=3.5*x,  Hence x is a multiple of 3.5
The expression multiplies an integer with 3.5.  See for more details: https://www.geeksforgeeks.org/multiply-an-integer-with-3-5/

Hence Option(B) is correct.

Question 3
What does the following C expression do?
 x = x & (x-1) 
Cross
Sets all bits as 1
Cross
Makes x equals to 0
Tick
Turns of the rightmost set bit
Cross
Turns of the leftmost set bit


Question 3-Explanation: 
The expression simply turns off the rightmost set bit. For example, if x = 14 (1110), x - 1 = 13 (1101) it returns resultant as (1100) i.e, 12 .
Question 4

Suppose we have a set {1,  2, 3}. Find the XOR of all the subsets of the set.

Cross

1

Cross

2

Cross

3

Tick

0



Question 4-Explanation: 

The XOR of all the subsets of any set is always 0 when n > 1 and Set[0] when n is 1. 

Because, When we apply XOR on all the subsets of a set, we can use the commutative and associative property of XOR which reduces the problem to finding XOR result of each element that depends on the total number of occurrences of each element. Eg. XOR([{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]) = XOR(XOR(1,1,1,1), XOR(2,2,2,2), XOR(3,3,3,3)) = 0

Let us consider n’th element, it can be included in the power set of remaining (n-1) elements. The number of subsets for (n-1) elements is equal to 2(n-1) which is always even when n>1. Thus, in the XOR result, every element is included even number of times, and the XOR of even occurrences of any number is 0.

Hence Option (D) is correct.

Question 5

Consider the following code snippet for checking whether a number is power of 2 or not. 

C

/* Incorrect function to check if x is power of 2*/
bool isPowerOfTwo (unsigned int x) 
{ 
  return (!(x&(x-1))); 
} 

What is wrong with above function?

Cross

It does reverse of what is required

Cross

It works perfectly fine for all values of x.

Tick

It does not work for x = 0

Cross

It does not work for x = 1



Question 5-Explanation: 

The logic behind this approach is that if a number is a power of 2, it will have only one bit set to 1 in its binary representation, and subtracting 1 from it will result in a binary number with all bits flipped to the right of that one bit. Performing a bitwise AND between these two numbers will result in 0, indicating that the number is a power of 2. 

However, there is a problem with this approach when the input is 0. In that case, the function will return true, even though 0 is not a power of 2. This is because 0 is the only number whose binary representation has no bits set to 1, so subtracting 1 from it will result in a binary number with all bits set to 1, and performing a bitwise AND between 0 and (0-1) will also result in 0. 

Therefore, the given function is incorrect as it does not handle the special case of 0. To fix this, we need to add a separate check for 0 at the beginning of the function and return false if the input is 0.

Please see https://www.geeksforgeeks.org/program-to-find-whether-a-no-is-power-of-two/

Hence Option (C) is the correct answer.

Question 6

What is the output of the following code snippet?

C++

#include <iostream>
using namespace std;

void fun(int& num, int k) { num &= (~(1 << k)); }
int main()
{
    int num = 7;
    int k = 1;
    fun(num, k);
    cout << num << endl;
    return 0;
}
Cross

It will unset the all bits of num

Cross

It will clear all the bits of bits

Tick

It will unset the kth bit of num

Cross

None



Question 6-Explanation: 

In the above code, we are doing the following things:

  • First, we left shift ‘1’ to n position via (1<<k) then we use bitwise NOT operator ‘~’ to unset this shifted ‘1’.
  • Now after clearing this left shifted ‘1’ i.e. making it to ‘0’ we will ‘AND'(&) with the number ‘num’ that will unset bit at the kth position.

Hence Option (C) is correct.

Question 7

what will do the following code in bit manipulation?

C++

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

It will return the last set bit in a number.

Cross

It will return the first set bit in a number.

Cross

It will xor of two numbers.

Tick

It will give the xor of numbers from 1 to N.



Question 7-Explanation: 

The above function is a direct method to calculate XOR of numbers from 1 to N.

Here are the steps to solve the problem:

  • Find the remainder of n by moduling it with 4. 
  • If rem = 0, then XOR will be the same as n. 
  • If rem = 1, then XOR will be 1. 
  • If rem = 2, then XOR will be n+1. 
  • If rem = 3, then XOR will be 0.

For more reference please visit: https://www.geeksforgeeks.org/calculate-xor-1-n/
Hence Option(D) is correct. 
 

Question 8

Right shift(>>) and Left shift(<<) are equivalent to _____ by 2.

Cross

Multiply and divide

Tick

Divide and multiply

Cross

Addition and subtraction

Cross

Subtraction and addition



Question 8-Explanation: 

The right shift operator is denoted by the double right arrow key (>>). The general syntax for the right shift is “shift-expression >> k”. The right-shift operator causes the bits in shift expression to be shifted to the right by the number of positions specified by k. Every time we shift a number towards the right by 1 bit it divides that number by 2.

The left shift operator is denoted by the double left arrow key (<<). The general syntax for left shift is shift-expression << k. The left-shift operator causes the bits in the shift expression to be shifted to the left by the number of positions specified by k. The bit positions that the shift operation has vacated are zero-filled. Every time we shift a number towards the left by 1 bit it multiply that number by 2.

Hence Option(B) is the correct answer.

Question 9

You are given a list of 5 integers and these integers are in the range from 1 to 6. There are no duplicates in list. One of the integers is missing in the list. Which of the following expression would give the missing number. ^ is bitwise XOR operator. ~ is bitwise NOT operator. Let elements of list can be accessed as list[0], list[1], list[2], list[3], list[4]

Cross

list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4]

Tick

|list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6

Cross

list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5

Cross

~(list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4])



Question 9-Explanation: 

XOR of all list elements and numbers from 1 to 6 gives the missing number. See this for details

Hence Option(B) is correct.
 

Question 10

If we want to invert all the bits of a number, then which bitwise operator should be used?

Tick

~

Cross

&

Cross

^

Cross

|



Question 10-Explanation: 

If we want to invert every bit of a number i.e. change bit ‘0’ to ‘1’ and bit ‘1’ to ‘0’.We can do this with the help of ‘~’ operator. For example: if number is num=00101100 (binary representation) so ‘~num’ will be ‘11010011’.

This is also the ‘1s complement of number’.

Hence option(A) is the correct answer.

There are 30 questions to complete.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads