Aspire Faculty ID #18812 · Topic: UGC NET Computer Science Nov 2021 (Paper II) · Just now
UGC NET Computer Science Nov 2021 (Paper II)

What does the following function $f()$ in C return?

int f(unsigned int N)
{
unsigned int counter = 0;

while (N > 0)
{
counter += N & 1;
N = N >> 1;
}

return counter == 1;
}

Solution

Given function takes an unsigned integer $N$.

Inside the function:

$counter=0$

Now the loop runs while:

$N>0$

In each iteration:

$counter += N\ \&\ 1$

Here, $N\ \&\ 1$ checks the last bit of $N$.

If the last bit is $1$, then $counter$ increases by $1$.

If the last bit is $0$, then $counter$ does not increase.

After that:

$N=N>>1$

This shifts the bits of $N$ one position to the right.

So, the loop checks all bits of $N$ one by one.

Therefore, $counter$ stores the number of $1$ bits in the binary representation of $N$.

Finally, the function returns:

$counter==1$

This condition is true only when the binary representation of $N$ contains exactly one $1$.

A number which has exactly one $1$ in binary form is a power of $2$.

For example:

$2=(10)_2$

$4=(100)_2$

$8=(1000)_2$

So, the function returns $1$ if $N$ is a power of $2$, otherwise it returns $0$.

Previous 10 Questions — UGC NET Computer Science Nov 2021 (Paper II)

Nearest first

Next 10 Questions — UGC NET Computer Science Nov 2021 (Paper II)

Ascending by ID
1
Consider the following recursive function $F()$ in Java that takes an integer value and returns a string value:public s…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
2
Match List I with List II:List IList IIA. JNDII. Runtime support for running Java programB. RMIII. The API in support o…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
3
Match List I with List IIList IList IIA. ImperativeI. Declarative, clausal representation, theorem provingB. Object-ori…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
4
Suppose you have eight black and white images taken with a $1$-megapixel camera and one $8$-color image taken by an $8$…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
5
Which of the statements given below are correct?The midpoint or Bresenham algorithm for rasterizing lines is optimized …
Topic: UGC NET Computer Science Nov 2021 (Paper II)
6
 What is the transformation matrix $M$ that transforms a square in the $xy$-plane defined by $(1,1)^T,(-1,1)^T,(-1…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
7
Suppose a Bezier curve $P(t)$ is defined by the following four control points in the $xy$-plane:$P_0=(-2,0),\ P_1=(-2,4…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
8
Given below are two statements:Statement I: The maximum number of sides that a triangle might have when clipped to a re…
Topic: UGC NET Computer Science Nov 2021 (Paper II)
9
In software testing, beta testing is the testing performed by ____________.
Topic: UGC NET Computer Science Nov 2021 (Paper II)
10
The V components in MVC are responsible for:
Topic: UGC NET Computer Science Nov 2021 (Paper II)
Ask Your Question or Put Your Review.

loading...