retagged by
311 views
7 votes
7 votes


Match the following expressions with the functions they are doing:

$
\begin{array}{|cc|cl|}
\hline
 A. & \text{!(n & n-1)} &1.&  \text{Sets the }{(d+1})^{th}\text{ bit from right of n to 1}\\ \hline
B. &  \text{n | (1<<d)}&2.& \text{Unsets the }{(d+1)}^{th}\text{  bit from right of n to 0}\\ \hline 
C. &  \text{n & (1<<d)}&3.& \text{Checks if n is a power of 2}\\ \hline
D. &  \text{ n ^ (1<<d)}&4.& \text{Complements the }{(d+1)}^{th}\text{  bit from right of n}\\ \hline 
 &  &5.& \text{Something else}\\ \hline   
\end{array}
$

  1. $A-3, B-1, C-2, D-4$
  2. $A-3, B-2, C -1, D-4$
  3. $A-3, B-1, C-5, D-4$
  4. $A-5, B-2, C-1, D- 3$

 

retagged by

1 Answer

Best answer
4 votes
4 votes
C is the correct answer.

$1 << d$ returns a number with ${(d+1)}^{th}$ bit from right side set to $1.$ So, bitwise OR with this will set the ${(d+1)}^{th}$ bit and bitwise XOR will complement it. But bitwise AND will also affect the other bits and it is not doing the unset operation.

if $n$ is a power of $2$ it'll have just one $“1"$ and all bits to its right will be $0's.$ And $n-1$ will have $1's$ in all those bits corresponding to the $0's$ in $n.$ So, $n\&(n-1)$ returns $0$ for any power of $2.$
selected by
Answer:

Related questions

4 votes
4 votes
1 answer
1
gatecse asked Jul 26, 2020
277 views
What will be the output of the following C program?#include<stdio.h int main() { int myinc = -1; if(++myinc || myinc) { myinc ; } printf("%d", myinc); }
2 votes
2 votes
1 answer
2
gatecse asked Jul 26, 2020
182 views
What will be the output of the following C program?#include<stdio.h int main() { int myinc = 0; if(++myinc && myinc) { myinc ; } printf("%d", myinc); }
5 votes
5 votes
1 answer
3
gatecse asked Jul 26, 2020
414 views
What will be the output of the following C program?#include<stdio.h int main() { int a, b = 2, c = 3; a = b, c++; printf("%d %d %d", a, b, c); }Compiler Error$3\; 3\; 4$$...
5 votes
5 votes
1 answer
4
gatecse asked Jul 26, 2020
293 views
if (x != 0) a = b; else a = c;What of the following can functionally substitute the if-else statement given above? a = (!!x)*b || c; a = b || c; a = (x>0)? c : b; a = (!!...