Thread: Decimal to binary recursively?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    37

    Decimal to binary recursively?

    I'm stuck. Can somebody help me with the algorithm to convert a decimal integer (0 - 1,000,000,000) to binary recursively? I know I'm going to %2 until it equals 0 or 1, but then what do I do?
    Last edited by skanxalot; 01-26-2003 at 12:20 PM.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Code:
    #include <iostream>
    
    using namespace std;
    
    void dectobin(int x)
    {
      if (x != 0)
      {
        dectobin(x >> 1);
      }
    
      cout<< !!(x & 1) <<flush;
    }
    
    int main()
    {
      dectobin(5);
    }
    Last edited by Cela; 01-26-2003 at 03:00 PM.
    *Cela*

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    OK, you're reply didn't make much sense to me as I think I'm quite a bit behind you. I've just been learning recursion for a week now. This is what I know:

    To find the binary of a decimal you do int division by 2 until it equals 1. Let's say I was doing the integer 9.

    9 --- 1
    4 --- 0
    2 --- 0
    1 --- 1

    So the binary is 1001.

    So for the recursive function my anchor or base is 1, right?

    So I use this to call it again? Recursive_Function(number/2)

    How then do I get the numbers off the stack to check for odd/even?

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Okay, ultra simple without any bitwise operations. :-) What you do is recurse deeper as long as the number isn't 0, when it is you print the modulus of the number you have and return.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void dectobin(int x)
    {
      if (x != 0)
      {
        dectobin(x / 2);
      }
    
      cout<< x % 2 <<flush;
    }
    
    int main()
    {
      dectobin(9);
    }
    The equivalent, more or less :-), with loops is like this
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void printbin(int x)
    {
      vector<int> bin;
    
      while (x != 0)
      {
        bin.push_back(x % 2);
        x = x / 2;
      }
    
      for (vector<int>::size_type i = 0; i < bin.size(); i++)
      {
        cout<< bin[i] <<flush;
      }
    }
    
    int main()
    {
      printbin(9);
    }
    *Cela*

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Cheers, mate!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM