528 views
1 votes
1 votes

Consider the following C function:

void foo(int n)
{
    while(n!=0)
    {
        if(!(n&1))
        printf("*");
        n=n>>1;
    }
}

The number of times printf (“*”) statement is executed, when the value $2^{24}$ is passed to the function foo().

1 Answer

1 votes
1 votes
24 is the answer

Last statement is right shift operator which means divide by 2 and the condition is bitwise AND with 1 which keep on resulting zero (!0=1 condition true) till the number is greater than 1. At 1 (1&1=1 condition becomes false) you can run the code for small example like 2^3 and printf will be executed 3 times.

Related questions

0 votes
0 votes
1 answer
1
0 votes
0 votes
1 answer
2
tishhaagrawal asked Dec 16, 2023
311 views
Below is my approach to solving this question, can anyone please explain if I am doing it the right way?Let X = #free slotssince, m =7 and n = 3So, $4 \leqslant x\leqsla...
0 votes
0 votes
1 answer
4
jugnu1337 asked Oct 22, 2023
333 views
Suppose A is a 12 by 9 incidence matrix from a connected (but unknown) graph with 9 nodes and 12 edges. The diagonal entries of $A^{T}.A$give the number of edges into eac...