Converting digit to binary using the POW function

This is a discussion on Converting digit to binary using the POW function within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm sorry I haven't had a chance to make a proper introduction yet, but I've been reading your ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Converting digit to binary using the POW function

    Hello everyone,

    I'm sorry I haven't had a chance to make a proper introduction yet, but I've been reading your forums for some time and I finally have a question that I need help on. Thanks in advance for any help

    I'm currently on an assignment where I have to convert a digit to binary. This task is a little different than most conversions (to binary, at least) since we're forbidden from using strings or arrays (so the binary number that gets returned is techinically an integer, which is why we won't be asked to convert a binary number higher than 100). I also must make use of the pow function, using a base of 10.

    I'll tell you what I have so far (I know it's horribly wrong, try not to laugh too hard ):

    Code:
    void getBinary(int number){
    
    int position=0;
    int tmp=0;
    
    while(number!=0){
    tmp=number%2+number%2*pow(10,position);
    number=number/2;
    position=position+1;
    }
    
    printf(" %d", tmp);
    
    }
    The problem is that the number I'm getting is just wrong. I know the method about getting a binary number (taking the modulus of your number, dividing it by two, taking the modulus of that number, etc...), I just don't know how to translate it to C-speak.

    It should also be mentioned that this isn't the entire code by the long shot. It's a really long assignment and this is only a part of it. This void function is called upon again at the end of the program, through use of another function. I've gotten the rest of the program to work besides this part, so any help would be greatly appreciated. Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You're supposed to take an int.... and translate it to an int. Am I missing something? I think you got your assignment all confused.

    If in code, you write this:

    Code:
    int x = 5;
    x contains 5... or 101 in binary. It's the same thing. Make sure you understand this, or you'll be trying to jam a square peg in a square hole and yelling to everyone that you can't figure out why the peg isn't coming out as a circle.

    When you print a number, you print it in any way you want. You can print it in binary, decimal, octal, or hexadecimal, or anything else. This works the same way as it does when you write a number on paper. You write it down as a symbol that is based off of a convention that tells you and/or your readers what you want them to understand.

    In hardware, most or all machines store numbers in binary form. This means 5 would be stored in binary. You can print it any way you want, though.

    So now I have to ask you.... exactly what is it that you're trying to do? Are you trying to print a given number using a binary representation? If that is the case, print it bit by bit. You can use shifting and bitwise operations that will make this pretty easy.

    You can't put a number in binary form into an int, and expect it to print back in binary if you give it to printf() and use %d as your format specifier.

    Anyway, enough ranting.... let me know if any of this makes sense.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Err, sorry MacGyver. I guess I should have made it more clear on what exactly is happening.

    As I said in my first post, that code up there isn't the entire code of my program. This particular part of the program is converting any integer (from 0 to 100) to its binary equivalent. Not shown here is the part where C asks you what number you want (which is in another part of the program--another function completely).

    So basically it's taking an integer and converting it into binary. I don't think we're on the same page, and I do indeed understand that C reads 5 as "binary", but the goal of this is to let the user take any value they want, and then the program will spit out what they want in binary.

    For example, this particular program will say "What digit do you want in binary?" The user will input "5" and then the program will say "5 in binary is 101." Similarly the user can ask "93" and the program will spit back "93 in binary is 1011101." Like I said, all of that isn't in this portion of the code, since it's called upon later.

    If it would be easier, I could paste all of the code for you, so you could see what's going on entirely. I'll just have to figure out how to copy and paste from Putty. Thanks.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I think you're right; we're not on the same page.

    Let's see.... So basically, let me guess what it is you're doing. You want to read in a number from the user... let's say.... 56. When getBinary() is called in this case, the variable passed to it will be 56, but you want 111000 to be printed.... So that tmp actually contains the decimal number 111,000.... Is this correct?

    If so, it's a weird assignment, but oh well.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Yes, that's correct. I know it's weird, but we are converting an integer to its binary equivalent, but that number is also an integer. This teacher just wanted us not to use strings and arrays. But yes, if the user said 56, the return would 111000.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That is messed up.

    Oh, well. Anyway took me a bit to figure it out, but I wrote out a test program, but I didn't really check it out with a lot of detail. This is the line you need to pay attention to:

    Code:
    tmp=number%2+number%2*pow(10,position);
    Anyway, math:

    Code:
    56 / 2 =	28	R-0
    28 / 2 = 	14	R-0
    14 / 2 = 	7	R-0
    7  / 2 = 	3	R-1
    3  / 2 = 	1	R-1
    1  / 2 = 	0	R-1
    So the idea is to get the remainder....

    So you want to build it backwards.

    Code:
    0
    00
    000
    1000
    11000
    111000
    You got the power thing along the right lines. You also have the remainder, diving-by-two thing right. What you're not doing right is saving the previous value of tmp. tmp should be as shown above, more or less.

    So anyway, here are some tips:

    1. Remember to add tmp to whatever result you're calculating... ie. tmp+=..... or tmp = tmp +....
    2. Don't add number%2 to the result. I think that is messing your calculations, or else I'm missing something.
    3. The pow() function has a slight problem; it returns a double. Since floats and doubles are approximations and not actual numbers, you'll eventually stumble on weird things. One of those weird things is having 10000.000000 converted to an int come out to 9999, which is probably throwing your calculations way off. I don't know what's going on there (well I sort of do, but it's hard to explain it without realizing how frustrating and silly it sounds) but calling ceil() on the result of pow() in this case corrects it.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    MacGyver, thanks SO much. The tmp = tmp + ... was the thing that was messing up the program, now it's working perfectly. And yes, the pow() function is limited, and my teacher mentioned that in class, I think. Anyway, we don't have to worry about that problem. I was getting the results that I needed to get, so thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 08:46 PM

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