The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 votes

Consider the C function func shown below: 

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

The value returned by func(0235) is ________

asked in Programming by Active (2.9k points)
edited by | 96 views

2 Answers

+5 votes
Best answer

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. 

answered by Veteran (406k points)
selected by
0 votes

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
answered by Junior (641 points)
So no matter how many number of 0's is present before it?
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.
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 before a decimal number doesn't mean anything.
It does mean in C language. It is for octal numbers.
0 for octal and 0x for hexadecimal like that?
yes.. And '0b' for binary but binary constant is not in C standard (only for GNU C).

Related questions

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
49,541 questions
54,083 answers
70,992 users