int log2(int n)
{
   int count = 1;
   
   while(!n >= 1)
   {
            if (n%2 == 0)
            {
            count++;
            n=n/2;
            }
            else
            {
            count++;
            n=n/2;
            floor(n);
            }
   
   }
   
   return count;
}

not sure why it isnt working?

I've changed your code a bit:

int log2(int n)
{
   int count = 1;
   
   while(n >= 1)
   {
	if (n%2 == 0)
	{
		count++;
		n=n/2;
	} else {
                count++;
                n=n/2;
                floor(n);
	}
   }
   
   return count-2;
}

I made changes to line 5 and to line 18 ...
On line 5 you're saying: while(!n >=1) well, actually the while is never run for values higher than or equal to 1 ...
Translated to human words: repeat this code IF n is NOT higher than 1 OR IF n is NOT equal to 1 ...
==> If you're dealing with values higher or equal to 1, the loop is never run as a result ...
Solution: Remove the '!'-sign and it works ...

In line 18: return count-2; because it was always returning values which were 2 higher than the actual solution ...

Hope this helps !

This question has already been answered. Start a new discussion instead.