edited by
16,470 views
34 34 votes

Consider the function func shown below: 

int func(int num) { 
   int count = 0; 
   while (num) { 
     count++; 
     num>>= 1; 
   } 
   return (count); 
} 

The value returned by func($435$) is ________

5 Answers

Best answer
59 59 votes

Answer is $9$.

$435-(110110011) $

num $>>=$ $1$; implies a num is shifted one bit right in every while loop execution.While loop is executed $9$ times successfully and $10$th time num is zero.

So count is incremented $9$ times.

Note:

Shifting a number "1"bit position to the right will have the effect of dividing by $2$:

8 >> 1 = $4    // In binary: (00001000) >> 1 = (00000100)
edited by
21 21 votes
int func(int num) // take decimal number
{ 
   int count = 0; 
   while (num) // until all bits are zero
   { 
     count++; // count bit 
     num>>= 1; // shift bits, removing lower bit
   } 
   return (count); // returns total number of bits
} 


(435)10 = (110110011)2 
So, the given program counts total number of bits in binary representation . Hence, answer is 9

http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer

reshown by
11 11 votes

The function mainly returns position of Most significant bit in binary representation of n. The MSB in binary representation of 435 is 9th bit.

Another explanation : >> in right shift. In other words, it means divide by 2. If keep on dividing by 2, we get: 435, 217, 108, 54, 27, 13, 6, 3, 1. Therefore, the count is 9.

edited by
3 3 votes

435 in binary ....................100110011...............count =0

first count is incremented then bitwise shift >>1 is done

count = 1 ............010011001

count = 2 ............001001100

count = 3 ............000100110

count = 4 ............000010011

count = 5 ............000001001

count = 6 ............000000100

count = 7 ............000000010

count = 8 ............000000001

count = 9 ............000000000

finally, count = 9

Answer:
Position:
Show:

Related questions

6 6 votes
3 3 answers
2.4k
2.4k views
Souvik33 asked Jan 15, 2023
2,414 views
Consider the function func shown below: int func(int num) { int count = 0; while (num) { count++; num>>= 1; } return (count); }The value returned by func(-435) is:69Will ...
51 51 votes
4 answers 4 answers
19.0k
19.0k views
go_editor asked Sep 28, 2014
18,978 views
Let $A$ be the square matrix of size $n \times n$. Consider the following pseudocode. What is the expected output?C=100; for i=1 to n do for j=1 to n do { Temp = A[i][j]+...
34 34 votes
4 answers 4 answers
16.5k
16.5k views
go_editor asked Sep 28, 2014
16,524 views
The number of distinct positive integral factors of $2014$ is _____________
51 51 votes
4 answers 4 answers
13.4k
13.4k views
go_editor asked Sep 28, 2014
13,438 views
Given an instance of the STUDENTS relation as shown as below$$\begin{array}{|c|c|c|c|c|} \hline \textbf {StudentID} & \textbf{StudentName} & \textbf{StudentEmail} & \text...