in Programming in C
543 views
1 vote
1 vote

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().

in Programming in C
543 views

1 comment

where you are facing problem ???
0
0

1 Answer

1 vote
1 vote
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