The Gateway to Computer Science Excellence
+17 votes
3.9k 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 ________

in Algorithms by
edited by | 3.9k views
0

will left shift will in multiplication?

0
yes
0
Operator is right shift. >>

5 Answers

+32 votes
Best answer

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)
by
edited by
0
doubt: why we are not considering the storing technique like little endian or big endian.. in C int take 16 bits if we consider big endian [uper bytes take lower address and lower bytes take upper address] then answer is fine but what if little endian...?
+14 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

by
reshown by
+6 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.

by
edited by
0
MSB not MSD
0 votes
Shift right of 1, which means the number gets half.
Shift left of 1, which means the number gets doubled.
In program, shift right of 1 is given, means every time we enter the loop the number will get halved, and the value of count will get incremented by 1. And when the value of num will become zero then the while loop will get terminated. So,
num = 435/2 = 217/2 = 108/2 = 54/2 = 27/2= 13/2 = 6/2 = 3/2 = 1/2 = 0
Count = 9
So, the value count that will get returned is 9.
by
0 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

by
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,484 answers
201,815 comments
95,291 users