what is the returning value

Consider the function func shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int func(int num)
 { 
int count = 0; 
 while (num) 
{ 
 count++; 
 num>>= 1; 
} 
return (count); 
} 

The value returned by func(435)is _ _ .


The answer is 9.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Anil Prajapati
Jan 30, 2015

int func (int num) { int count = 0; while (num) //After each right shift, checks whether the num value is not zero// { count ++; num ≫= 1; //shifts all bits of num one slot to the right// } return(count); } Initially num = 110110011, count = 0 count = 1; num = 101100110 after 1st right shift count =2; num = 011001100 after 2nd right shift : : Count = 9; num = 000000000 after 9th right shift. After nine right shifts, num = 0; and while loop terminates count = 9 will be returned.

Vijay Kumar
Jan 24, 2016

This procedure is to count the total bits in the binary representation of the number.

The nearest 2th power to 435 is 256 which can be represented as 2^8 and hence numbers upto 2^8 - 1 will be represented using 8 bits.

So 435 being greater than 2^8 - 1 , it is being represented using 9 bits and hence the procedure will return 9. This could be done without any programming language, once you get what the procedure is going to do.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...