Top MCQs on Bitwise Algorithms and Bit Manipulations with Answers (original) (raw)
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; }
C
int fun(int arr[], int n) { int x = arr[0]; for (int i = 1; i < n; i++) x = x ^ arr[i]; return x; }
Java
public int fun(int[] arr) { int x = arr[0]; for (int i = 1; i < arr.length; i++) x = x ^ arr[i]; return x; }
Python
def fun(arr): x = arr[0] for i in range(1, len(arr)): x ^= arr[i] return x
JavaScript
function fun(arr) { let x = arr[0]; for (let i = 1; i < arr.length; i++) x ^= arr[i]; return x; }
`
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
What does the following expression do?
x = x & (x-1)
- Turns of the rightmost set bit
- Turns of the leftmost set bit
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))); }
C
/* Incorrect function to check if x is power of 2*/ bool isPowerOfTwo (unsigned int x) { return (!(x&(x-1))); }
Java
// Incorrect function to check if x is power of 2 public static boolean isPowerOfTwo(unsigned int x) { return (x & (x - 1)) == 0; }
Python
Incorrect function to check if x is power of 2
def is_power_of_two(x): return (x & (x - 1)) == 0
JavaScript
// Incorrect function to check if x is power of 2 function isPowerOfTwo(x) { return (x & (x - 1)) === 0; }
`
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
What is the output of the following code snippet?
C++ `
#include 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; }
C
#include <stdio.h>
void fun(int* num, int k) { *num &= (~(1 << k)); } int main() { int num = 7; int k = 1; fun(&num, k); printf("%d\n", num); return 0; }
Java
public class Main { public static void fun(int[] num, int k) { num[0] &= (~(1 << k)); } public static void main(String[] args) { int[] num = {7}; int k = 1; fun(num, k); System.out.println(num[0]); } }
Python
def fun(num, k): num[0] &= ~(1 << k)
num = [7] k = 1 fun(num, k) print(num[0])
JavaScript
function fun(num, k) { num[0] &= ~(1 << k); }
let num = [7]; let k = 1; fun(num, k); console.log(num[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
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; }
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; }
Java
public 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; }
Python
def function(n): if n % 4 == 0: return n if n % 4 == 1: return 1 if n % 4 == 2: return n + 1 else: return 0
JavaScript
function function(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.
Right shift(>>) and Left shift(<<) are equivalent to _____ by 2.
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])
If we want to invert all the bits of a number, then which bitwise operator should be used?
What will the below code snippet do?
C++ `
#include using namespace std; int main() { int num = 5; cout << (~num + 1) << endl; return 0; }
C
#include <stdio.h> int main() { int num = 5; printf("%d\n", (~num + 1)); return 0; }
Java
public class Main { public static void main(String[] args) { int num = 5; System.out.println(~num + 1); } }
Python
num = 5 print(~num + 1)
JavaScript
let num = 5; console.log(~num + 1);
`
- It will print 2's complement of a number.
- It will print 1.s complement of a number.
There are 30 questions to complete.
Take a part in the ongoing discussion