Solved

Converting Decimal to Binary

Posted on 2004-08-05
6
3,896 Views
Last Modified: 2013-11-15
My program transforms decimal numbers to binary representation.  But I need it to transform it to 16 bit binary representation, mine only transforms to regular binary.
For example,  Mine: 25 = 11001 but it should be 0000000000011001.  This is my program:
#include <iostream>

using namespace std;

void dectobin(int num, int base);

int main()
{
      int decimalnum;
      int base;

      base = 2;

      cout<<"Enter the number in decimal: ";
      cin>>decimalnum;
      cout<<endl;
      cout<<"Decimal: "<<decimalnum<<" = ";
      dectobin(decimalnum, base);
      cout<< " Binary" <<endl;

      return 0;

}
void dectobin(int num, int base)
{
      if(num > 0)
      {
            dectobin(num/base, base);
            cout<<num % base;
      }
}
What do I have to do to make it print out the remaining digits?  Please Advise
0
Comment
Question by:Steve3164
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11728066
You could easily do that by switching from a recursive function to a 'linear one, e.g.

void
dectobin (unsigned int b) {

unsigned int mask, count;

      for ( count = 0, mask = 0x80000000; mask != 0; mask = ( mask >> 1)) {

            if ( b & mask) {

                  cout << 1;

            } else {

                  cout << 0;
            }
      }
}
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 11728199
Try this:

void dectobin(int num, int base, int count = 0);

int main()
{
     int decimalnum;
     int base;

     base = 2;

     cout<<"Enter the number in decimal: ";
     cin>>decimalnum;
     cout<<endl;
     cout<<"Decimal: "<<decimalnum<<" = ";
     dectobin(decimalnum, base);
     cout<< " Binary" <<endl;
     cin >> base;
     return 0;

}
void dectobin(int num, int base, int count)
{
     if(num > 0 || count < 16)
     {
          dectobin(num/base, base, ++count);
          cout<<num % base;
          char c2[] = "0";
     }
}

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11728225
You may remove these lines as i needed them on my IDE only:

>>     cin >> base;

>>           char c2[] = "0";

Regards, Alex

         
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:rstaveley
ID: 11730745
std::bitset<> is nice for this:
--------8<--------
#include <iostream>
#include <bitset>

int main(int argc,const char *argv[])
{
      std::cout << std::bitset<16>(atoi(*++argv)) << '\n';
}
--------8<--------
0
 

Expert Comment

by:H_Kazemi
ID: 11745686
Hi steve3164!
It is far better to use a direct method to do this rather simple function instead of a recursive one; because as you may know recursive functions are very inefficient due to their excessive use of stack memory as well as their low speed.
This piece of code can do what you need. I have used unsigned short int instead of unsigned int because you are using 16 bit data:

#include <iostream.h>

void dectobin(unsigned short int num);

void main(void)
{
    unsigned short int number;
    cout<<"Enter a decimal integer: ";
    cin>>number;
    cout<<"\nBinary representation: ";
    dectobin(number);
    cout<<endl;
}

void dectobin(unsigned short int num)
{
    unsigned int powerOf2=0x8000;
    while (powerOf2!=0)
    {
        if (num>=powerOf2)
        {
            num^=powerOf2;  //Equivalent to num-= powerOf2;
            cout<<1;
        }
        else
            cout<<0;
        powerOf2=powerOf2>>1;
    }
}
0
 

Author Comment

by:Steve3164
ID: 11747820
thanks itsmeandnobodyelse!!!

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Software for IT quotations, invoices, delivery orders 2 63
Installshield for Embarcadero EX 10.1 Berlin 4 74
How do i run a c++ file? 15 52
moto 4g plus phone apps 5 51
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question