Link to home
Start Free TrialLog in
Avatar of Steve3164
Steve3164

asked on

Negative Dec to Binary

Below is my program that reads decimal numbers and outputs them in binary code.  Know I am trying to read negative numbers and output them in 2's complement form.  For example, 8 is 0000000000001000 but -8 is 1111111111111000 in 2's complement form.  How can I do this.  I tried changing the <> but this did not work.  
my input has these numbers 252,8,9,45,63,74 (uses these numbers for negative as well).

#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
      ifstream inFile;
      inFile.open("input3.txt");
      
      int decimalnum[6];
      int base;
      int i;
      base = 2;
      
      cout<<"Enter the number in decimal: "<<endl;
      if(!inFile)
      {
            cout<<"Cannot open input file!"<<endl;
            return 1;
      }
      else
            for (i=0 ;i<6;i++) {
                  inFile>>decimalnum[i];
                  cout<<"Decimal: "<<decimalnum[i]<<" = ";
                  dectobin(decimalnum[i], base);
                  cout<< " in Binary" <<endl;
            }
            
            return 0;
            
}
void dectobin(int num, int base, int count)
{
      if(num > 0 || count < 16)
      {
            dectobin(num/base, base, ++count);
            cout<<num % base;
            
      }
      
}
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Just manage the number always as unsigned

unsigned int unum = *((unsigned int *)yournumber);

That will catch your negative number without trying to make any conversion. If you print unum as usual will show you 2's complement.
jaime has it right.

As a binary representation always is unsigned, change your prog that way:

#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
     ifstream inFile;
     inFile.open("input3.txt");
     
     int decimalnum[6];
     int base;
     int i;
     base = 2;
     
     cout<<"Enter the number in decimal: "<<endl;
     if(!inFile)
     {
          cout<<"Cannot open input file!"<<endl;
          return 1;
     }
     else
          for (i=0 ;i<6;i++) {
               inFile>>decimalnum[i];
               cout<<"Decimal: "<<decimalnum[i]<<" = ";
               dectobin(decimalnum[i], base);
               cout<< " in Binary" <<endl;
          }
         
          return 0;
         
}
void dectobin(int inp, int base, int count)
{
     unsigned int num = (unsigned int)inp;
     if(num > 0 || count < 16)
     {
          dectobin(num/base, base, ++count);
          cout<<num % base;
         
     }
     
}


Regards, Alex

Avatar of Steve3164
Steve3164

ASKER

Alex,  

Your information help out but it outputs 32 bits for the negative and I need it to output only 16.  For example, -8 = 1111111111111000 but I get
11111111111111111111111111111000.  How can I do this?
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe I have not contributed to solve this problem?