1. ## log(2) function HELP

Hey guys! Im new to the forums and just started a programming course in college and im having some difficulties. The exercise is as followed:

The binary logarithm of n (log2n) is the power to which the number 2 to must be raised to obtain the value n. Write a C function that calculates log2 of an unsigned integer n using the following approach: find the most significant set bit in n and return the position of this bit. For example, if n is 17 (0b10001), the function should return 4.
The C function should have the following signature:

int mylog2(unsigned int n)

I'm not sure what to do and what "find the most significant set bit in n and return the position of this bit" means. And also how to I can do this, I certainly dont want u guys to do this for me, but some info on how to do it would be appreciated.

I have asked my teacher for help, but he just said read the goddamn books, but I have and still dont know the steps to solve this. Thanks

2. n is going to be in decimal or in binary?If decimal,google to find how to convert from dec to binary.Then the most significant bit,is the bit that is the first bit in the sequence of bits .So take a look at your example
10001.The least significant bit is the pink(whatever) 1,which is in position zero.The zero next to it,is in the 1st position and so on.So the red 1,is the most significant bit in this case,which is located in position no. 4

Edit: WELcOmE to the forum

3. Ah ty, i get that now!

As for the rest, how should i go about the exercise? Does C have an log(2) build in function?

4. I do not remember now actually,but if it has it should be in math.h header.well now you got to write some code and post back if you have problems.As i said,n is in binary or in decimal?If in decimal you have to convert it in binary!If not,then you have to locate it's most significant bit Already told much,try your own and do not hesitate to post back,but you should give it a shot

5. There's certainly no reason to convert from decimal to binary. Presumably you've covered the bitshifting operator >>.

Since the value is unsigned, you could simply shift it until it becomes 0, counting the number of times you shifted it.

The C standard library doesn't have a log base 2 function, but it's easy to construct out of the natural log function log (or even the log base 10 function log10).
And I agree with your teacher that you should read the books.

6. Hello again. After hours and hours and with help from my math teacher I was able to make it.
Code:
```#include <stdio.h>
#include <stdlib.h>

int mylog2(unsigned int n)

{
printf("calculate n(log2n)\n");
scanf("%d", &n);
int m;
int logn = -1;
for (m = n; m>0; m = m>>1 ) logn++;
if (logn == -1) {
printf("ERROR: Zero and Negative number is illegal, undefined.\n");
exit(1);
}
return logn;

}

int main (unsigned int n)
{
int k = mylog2(n);
printf("log2 is %d\n", k);
return 0;
}```

7. By the way, I don't really understand this code to the point where I can write down step by step what is going on. So could someone please enlighten me??

8. That would best be an exercise for yourself. Try it, line by line. If you get stuck on something particular, show us what you've figured out so far and where you're having trouble.

9. Oh i am going to explain step by step when you explain me why your main receives an agrument ?main is supposed to get no arguments
Code:
` int main(void)`
or it can get two arguments to handle command line but let this for now.So declare the variable n and switch main to void like this
Code:
```int main(void)
{unsigned int n;
....
}```
A suggestion i have to make is that in most cases the prototypes of the functions are before main(declaration),then comes the main and then there are the definition of the functions.For instance
Code:
```int foo(void);/*declaration*/
void bla(int n);/*declaration too*/

int main(void)
{
...
return 0;
}

int foo(void)/*definition*/
{
/*body of the function*/
}

void bla(int n)/*definition*/
{
/*body of the function*/
}```
Also i would suggest you to read the input,with scanf,before passing n as an argument.In code
Code:
```int foo(int n);

int main(void)
{
printf("calculate n(log2n)\n");
scanf("%d", &n);
foo(n);
...
return 0;
}
int foo(int n)
{
...
}```
The goal is to have tiny functions that two one job.This way you give your program a real boost(escpecially when its large).

Actually you could even create a function that read the data!And why not another one that would print the result?Let foo be the function that does all the computations..Check this example
Code:
```#include<stdio.h>

int input(int n);
int foo(int n);

int main(void)
{
int n;
int result;
n=input(n);
result=foo(n);
print(result);
return 0;
}

int input(int n)
{
printf("calculate n(log2n)\n");
scanf("%d", &n);
return n;
}

int foo(int n)
{
int m;
int logn = -1;
for (m = n; m>0; m = m>>1 ) logn++;
if (logn == -1) {
printf("ERROR: Zero and Negative number is illegal, undefined.\n");
exit(1);    /*i do not like this.you may write return -1
and have in mind to check in main if the functions returns -1.If so then an error has occured(actually logn is equal to -1)
}
return logn;
}

int print(int n)
{
printf("log2 is %d\n", k);
}```
and actually i think that is a nice idea to split the input function in two functions.One that print the welcome messages and another that actually reads the data!
Code:
```void printWelcomeMessages(void)
{
printf("calculate n(log2n)\n");
}

int input(int n)
{
scanf("%d", &n);
return n;
}

int getInput(int n)
{
printWelcomeMessages();
return input(n);
}```
Also i have to display an example about the return -1 thing i told(i suggest you to use it instead of exit(1)).One reason that comes to mind,is that in my way you are able to handle the error your own

Code:
```#include <stdio.h>

int input(int n);
int getInput(int n);

int main(void)
{
int n;
/*Let's say that you want to have a positive variable n*/
if(getInput(n) == -1 )
{
/*do error handle here*/
printf("Number is going to set to zero.\n");
n=0;
}
return 0;
}

int getInput(int n)
{
n=input(n);
if(n<=0)
{
return -1;
}
else
{
return n;
}
}```

10. Originally Posted by Matticus
That would best be an exercise for yourself. Try it, line by line. If you get stuck on something particular, show us what you've figured out so far and where you're having trouble.
He is right

11. Code:
```int foo(void);/*definition*/
void bla(int n);/*definition too*/```
These are referred to as "function declarations" (the definitions go below, where they are defined with code).

12. Originally Posted by Matticus
These are referred to as "function declarations" (the definitions go below, where they are defined with code).
Oh my god,of course you are right!Actually if you read more,you would see that i have confused the names.I am going to edit Thanks!

13. Originally Posted by patta
Hello again. After hours and hours and with help from my math teacher I was able to make it.
Code:
```#include <stdio.h>
#include <stdlib.h>

int mylog2(unsigned int n)

{
printf("calculate n(log2n)\n");
scanf("%d", &n);
int m;
int logn = -1;
for (m = n; m>0; m = m>>1 ) logn++;
if (logn == -1) {
printf("ERROR: Zero and Negative number is illegal, undefined.\n");
exit(1);
}
return logn;

}

int main (unsigned int n)
{
int k = mylog2(n);
printf("log2 is %d\n", k);
return 0;
}```
Your function is badly designed. You shouldn't be printing or scanning anything in it. Just pass in the number and calculate the result. Let it return -1 as an error condition instead of printing the error message and exiting in the function, i.e., let the caller decide how to handle the error.

So print the prompt and read the input number in main, then pass it into the function, then check the return value for the error code (-1) back in main again.

14. Originally Posted by oogabooga
Your function is badly designed. You shouldn't be printing or scanning anything in it. Just pass in the number and calculate the result. Let it return -1 as an error condition instead of printing the error message and exiting in the function, i.e., let the caller decide how to handle the error.

So print the prompt and read the input number in main, then pass it into the function, then check the return value for the error code (-1) back in main again.
Excuse me oogabbooga,with all the kindness-you now that i like your name and the way you answer-but you are not saying anything more than what i said i suppose...So adding a new post with no new information? Just a thought.Again ,i mean it, with all the kindness i have + 1 (an overflow may occur now )

15. Originally Posted by std10093
Excuse me oogabbooga,with all the kindness-you now that i like your name and the way you answer-but you are not saying anything more than what i said i suppose...So adding a new post with no new information? Just a thought.Again ,i mean it, with all the kindness i have + 1 (an overflow may occur now )
Sorry std. I admit I didn't read your post until after I had posted mine and I did realize that you said the same things. Laziness, I suppose. It happens.