Like Tree3Likes

log(2) function HELP

This is a discussion on log(2) function HELP within the C Programming forums, part of the General Programming Boards category; Hey guys! Im new to the forums and just started a programming course in college and im having some difficulties. ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    5

    Question 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. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    Last edited by std10093; 09-19-2012 at 12:54 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    5
    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. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    5

    Cool

    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"); 
    printf("Type your number:\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;     
    }
    Is there any suggestions about this code? Is it as simple as it can be?
    Last edited by patta; 09-20-2012 at 08:44 AM.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Location
    Norway
    Posts
    5
    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. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,264
    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. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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"); 
    printf("Type your number:\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"); 
    printf("Type your number:\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"); 
        printf("Type your number:\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*/
    printf("Please,input a positive number\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;
          }
    }
    Last edited by std10093; 09-20-2012 at 10:36 AM.
    oogabooga and patta like this.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Matticus View Post
    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. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,264
    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. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Matticus View Post
    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. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by patta View Post
    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"); 
    printf("Type your number:\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;     
    }
    Is there any suggestions about this code? Is it as simple as it can be?
    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.
    laserlight likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by oogabooga View Post
    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. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by std10093 View Post
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-07-2012, 04:35 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 04:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 12:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21