0 votes 0 votes int total(int v) { static int count = 0; while(v) { count += v&1; v >>= 1; } return count; } // input = 5. Why is the while loop running only one time.Shoudnt it run 3 times? Programming in C programming-in-c + – Angkit asked Jun 12, 2017 Angkit 740 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Rupendra Choudhary commented Jun 13, 2017 reply Follow Share who said you it will run one time? while (5) { c=0+ (0101&0001)=0+1=1 // Bitwise AND v= right shift by 1 =>00000101 so now v=0010=2 } while(2) { c=1+(0010&0001)=1+0=1 v= right shift by 1 =0001=1 } while(1) { c=1+(0001&0001)=1+1=2 v=right shift by 1=0000=0 } while (0) //Exit 1 votes 1 votes Angkit commented Jun 13, 2017 reply Follow Share thnx...@ Rupendra 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes It will run 3 times.If we do right shift of 5 by one we will get 2 then again if we do right shift of 2 we get 1 and if we do right shift of 1 we get zero so the while loop is true for 5,2,1.Totally 3 times it enter in to loop Niharika 1 answered Jun 13, 2017 Niharika 1 comment Share Follow See all 0 reply Please log in or register to add a comment.