411 views
2 votes
2 votes
#include<stdio.h>
int calculate(int n)
{
    int count=0;
    while(n!=0){
    n=(n&(n<<1));
    count++
    }
  return count;  
    
}

output for calculate(143)

1 Answer

Best answer
2 votes
2 votes

4 should be answer .

n=143  , 143 <<1 = 286 ,  (143&286) = 14  , therefore n =14 ;

count =1  now 

14 <<1 = 28 , so (14&28) = 12  , n=12

count =2 ;

12<<1  = 24  ,  (24&48) =  16  , n =16

count = 3;

16<<1 = 32  , (16&32) =  0

count  =4 

, now n =0  

next time loop will exit and    so, count =4 

selected by

Related questions

0 votes
0 votes
1 answer
1
Rackson asked Jan 12, 2019
956 views
0 votes
0 votes
1 answer
2
0 votes
0 votes
0 answers
3
0 votes
0 votes
1 answer
4