131 views

Consider the C function func shown below:

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

The value returned by func(0235) is ________

edited | 131 views

In C language an integer constant starting with a 0 is treated as octal. So, $0235 = 157$ in decimal. Now, the code is giving the position of the most significant bit in the binary representation of the number. $157 = (10011101)_2$, so, MSB is at position 8.

by
selected by
8

Binary of 235:11101011

Now doing right shift and incrmenting by 1, means essentially counting the number of bits in the binary version of 235, which is 8.

So ans is 8
by
0
So no matter how many number of 0's is present before it?
0
Before what? The number is already given , find binary and count number of bits.That's it! That is exactly what the program is doing.
0
i mean before any number. like for 0235 it is 11101011 and not 011101011. what if the number inside func() is 0. ie func(0)
0
0 before a decimal number doesn't mean anything.
+2
It does mean in C language. It is for octal numbers.
0
0 for octal and 0x for hexadecimal like that?
+2
yes.. And '0b' for binary but binary constant is not in C standard (only for GNU C).