740 views
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?

1 Answer

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

Related questions

1 votes
1 votes
1 answer
2
jugnu1337 asked May 10, 2022
260 views
SOURCE NAME::: UNDERSTANDING POINTER IN C (BOOK) BY YASHWANT KANETKARmy answer is C am i right?
1 votes
1 votes
0 answers
3
aakash pandey asked Apr 30, 2022
281 views
We use character array to declare string in C. So, if I declare an array likecharacter ch[ ] = {‘a’,’k’,’a’,’/o’};and try printing ch[3] like :printf(“%...
0 votes
0 votes
0 answers
4
Anirudh Kaushal asked Apr 6, 2022
191 views
#include<stdio.h struct marks { int p:3; int c:3; int m:2; }; void main() { struct marks s = {2, -6, 5}; printf("%d %d %d", s.p, s.c, s.m); }What does p:3 means here ?