• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Top MCQs on Bitwise Algorithms and Bit Manipulations with Answers

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;
}
  • 0

  • 9

  • 12

  • 2

Question 2

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

  • Multiplies an integer with 7

  • Multiplies an integer with 3.5

  • Multiplies an integer with 3

  • Multiplies an integer with 8

Question 3

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

Question 4

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?

  • It does reverse of what is required

  • It works perfectly fine for all values of x.

  • It does not work for x = 0

  • It does not work for x = 1

Question 5

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;
}
  • It will unset the all bits of num

  • It will clear all the bits of bits

  • It will unset the kth bit of num

  • None

Question 6

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;
}
  • It will return the last set bit in a number.

  • It will return the first set bit in a number.

  • It will xor of two numbers.

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

Question 7

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

  • Multiply and divide

  • Divide and multiply

  • Addition and subtraction

  • Subtraction and addition

Question 8

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]

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

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

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

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

Question 9

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

  • ~

  • &

  • ^

  • |

Question 10

What will the below code snippet do?

C++
#include <iostream>
using namespace std;
int main()
{
    int num = 5;
    cout << (~num + 1) << endl;
    return 0;
}
  • It will print 2's complement of a number.

  • It will print 101.

  • It will print 1.s complement of a number.

  • None

There are 30 questions to complete.

Last Updated :
Take a part in the ongoing discussion

Trending in News

View More