Solved

Negative Dec to Binary

Posted on 2004-08-08
5
432 Views
Last Modified: 2012-06-27
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;
            
      }
      
}
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
  • 2
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11747860
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11748605
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

0
 

Author Comment

by:Steve3164
ID: 11749818
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?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 400 total points
ID: 11751051
Sorry, my fault.

Try that:

void dectobin(int inp, int base, int count)
{
     // first cast to 16 bits signed, then to 16 bits unsigned
     unsigned short num = (unsigned short)(short)inp;
     if(num > 0 || count < 16)
     {
          dectobin(num/base, base, ++count);
          cout<<num % base;
         
     }
}

Regards, Alex
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11756719
Maybe I have not contributed to solve this problem?
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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