Answer is 23
Value of v is in binary
Each iteration in while loop updates count if LSB of v has 1 else count remains unchanged and shift right is performed on v
Each iteration of for loop updates value of x as previous(x)+currentvalue(count)
Value of i
|
Value of x
|
while loop
iteration no.
|
Value of v
|
Value of count
|
5 |
0 |
0 |
101 (before loop starts) |
0 |
|
0 |
1 |
101 |
1 |
|
0 |
2 |
10 |
1 |
|
0 |
3 |
1 |
2 |
|
0+2= 2 |
|
|
|
4 |
2 |
0 |
100 |
2 |
|
2 |
1 |
100 |
2 |
|
2 |
2 |
10 |
2 |
|
2 |
3 |
1 |
3 |
|
2+3= 5 |
|
|
|
3 |
5 |
0 |
11 |
3 |
|
5 |
1 |
11 |
4 |
|
5 |
2 |
1 |
5 |
|
5+5= 10 |
|
|
|
2 |
10 |
0 |
10 |
5 |
|
10 |
1 |
10 |
5 |
|
10 |
2 |
1 |
6 |
|
10+6= 16 |
|
|
|
1 |
16 |
0 |
1 |
6 |
|
16 |
1 |
1 |
7 |
0 exits for loop |
16+7=23 |
|
|
|