Question

In: Computer Science

i need to convert decimal to twos complement binary and then add the binary digits but...

i need to convert decimal to twos complement binary and then add the binary digits but I am unable to do it. I am only allowed to use string, can anyone help me out please. i need the code urgently.

#include
#include
#include
#include

using namespace std;


string reverse(string s)
{
string x = "";
for (long i = s.length() - 1; i >= 0; i--)
{
x += s[i];
}
return x;
}
string twosComplementStringsAddition(string A, string B)
{
string c;
int count = 0;
for (int i = 0; i < A.length(); i++)
{
string c;
c.resize(A.length());
if (count > 0)
c[i] = '1';
else if (A[i] == '1' & B[i] == '1')
{
c[i] = '0';
count++;
}
else if ((A[i] == '1' & B[i] == '0') || (A[i] == '0' & B[i] == '1'))
c[i] = '1';
else if (A[i] == '0' & B[i] == '0')
c[i] = '0';
count = 0;
}
return c;
}
string negative(string a)
{
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '1')
a[i] = '0';
else
a[i] = '1';
}
reverse(a);
string x = "1";
twosComplementStringsAddition(a, x);
reverse(a);
return a;
}
string decimalToTwosComplementString(int a, int L)
{
{
  
for (int i = L - 1; i >= 0; i--)
{
int k = a >> i;
if (k & 1)
cout << "1";
else
cout << "0";
}
}
string s = " ";
L = s.length();
int b;
for (int i = 0; i < L - 1; i++)
{
b = a % 2;
if (b == 1)
s[i] = '1';
else if (b == 0)
s[i] = '0';
a = a / 2;
}
reverse(s);
if (a < 0)
{
negative(s);
return s;
}
else
return s;
}
int twosComplementStringToDecimal(string a)
{
int result = 0;
if (a[0] == '0')
{
reverse(a);
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '1')
{
result = result + pow(2, static_cast(i));
}
else if (a[i] == '0')
{
result = result + 0;
}
}
}
else
{
negative(a);
int resulta = 0;
reverse(a);
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '1')
{
resulta = resulta + pow(2, static_cast(i));
}
else if (a[i] == '0')
{
resulta = resulta + 0;
}
}
return resulta;
}
return result;
}
int main()
{
   //Read in the bit pattern size
   int L;
   do
   {
       cout << "Enter positive integer for the bit pattern size ";
       cin >> L;
   } while (L <= 0);

   //Read in two integers a and b
   int a, b;
   cout << "Enter an integer a ";
   cin >> a;
   cout << "Enter an integer b ";
   cin >> b;

   //Calculate the decimal arithmetic sum of a and b and print the result
   int c1 = a + b;
   cout << "In decimal " << a << " + " << b << " is " << c1 << endl;

   //Compute the two's complement representations of a and b
   //Each integer must be represented in L-bits pattern
   //Also these two's complement representations must be returned as string data types
   string A = decimalToTwosComplementString(a, L);
   string B = decimalToTwosComplementString(b, L);

   //Print the two's complement representations of a and b
   cout << "The two's complement of " << a << " is\t " << A << endl;
   cout << "The two's complement of " << b << " is\t " << B << endl;

   //Compute the binary sum of the two's complement representations of a and b
   //The result must be returned as L-bit pattern string data type
   string C = twosComplementStringsAddition(A, B);

   //Print the two's complement representation binary sum
   cout << "The binary sum of " << A << " and " << B << " is " << C << endl;

   //Convert the two's complement representation binary sum to decimal and print
   int c2 = twosComplementStringToDecimal(C);
   cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;

   //Print some concluding results
   if (c1 == c2)
       cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
   else
   {
       cout << c1 << " is not equal to " << c2 << endl;
       cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made a mistake!" << endl;
   }
   system("Pause");
   return 0;
}

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Source Code:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

//Helper function:convert the original decimal number to unsigned binary number.
string decimalToUnsign(int a, int L)
{
   string result = "";
   int r;
   while (a>0)
   {
       r = a%2;
       if(r == 1)
           result = "1" + result;
       else
           result = "0" + result;
       a /= 2;
   }
   int n = result.length();

   if (L >= n) {
       for(int i=0; i<L-n; i++)
           result = "0" + result;
          
   }
   else
   {
       string temp ="";
       for(int j = n-L; j < n; j++)
           temp += result[j];
       result = temp;
   }

   return result;
}

//Function 1
string decimalToTwosComplementString(int a, int L)
{
   string result = "";
  
   if (a >= 0) {
       result = decimalToUnsign(a, L);
   }
   else
   {
       a = a * -1;
       result = decimalToUnsign(a, L);
       //flip
       string temp ="";
       for(int k=0; k<L; k++)
       {
          
           if (result[k]=='0')
               temp += "1";
           else
               temp += "0";
       }
       result = temp;
       //add 1
       string temp1 = "";
       for(int i = L-1; i>=0; i--)
       {
           if (result[i] == '0')
           {
               temp1 = "1" + temp1;
               break;
           } else {

               temp1 += "0";
           }  
       }
       int size = temp1.length();
       for(int i=L-size-1; i>=0; i--)
       {
           temp1 = result[i] + temp1;
       }
       result = temp1;
   }
   return result;
}

//Function 2
int twosComplementStringToDecimal(string C)
{
   int num = 0;
  
   if (C[0] == '0')
   {
       int index = 0;
       for(int i= C.length()-1; i>=0; i--)
       {
           if(C[C.length()-1] == '1' && i == C.length()-1)
               num = 1;

           else if(C[i] == '1')
           {
               int temp = 1;
               for(int j = 0; j<index; j++)
                   temp *= 2;
               num += temp;
           }
           index++;
       }
   }
   else
   {
       string st ="";
       for(int k=0; k < C.length(); k++)
       {
           if (C[k]=='0')
               st += "1";
           else
               st += "0";
       }

       C = st;
      
       //add 1
       string temp1 = "";
       for(int i = C.length()-1; i>=0; i--)
       {
           if (C[i] == '0')
           {
               temp1 = "1" + temp1;
               break;
           }
           else
           {
               temp1 += "0";
           }  
       }
       int size = temp1.length();
       for(int i=C.length()-size-1; i>=0; i--)
       {
           temp1 = C[i] + temp1;
       }

       C = temp1;

       int index = 0;
       for(int i= C.length()-1; i>=0; i--)
       {
           if(C[C.length()-1] == '1' && i == C.length()-1)
               num = 1;

           else if(C[i] == '1')
           {
               int temp = 1;
               for(int j = 0; j<index; j++)
                   temp *= 2;
               num += temp;
           }
           index++;
       }
       num*= -1;
   }
   return num;
}

//Function 3
string twosComplementStringsAddition(string A, string B)
{
return decimalToTwosComplementString(twosComplementStringToDecimal(A) + twosComplementStringToDecimal(B), A.length());
}

//Main function code provided by Mr.Koopa :)
int main()
{
   //Read in the bit pattern size
   int L;
   do
   {
       cout << "Enter positive integer for the bit pattern size ";
       cin >> L;
   }while (L <= 0);

   //Read in two integers a and b
   int a, b;
   cout << "Enter an integer a ";
   cin >> a;
   cout << "Enter an integer b ";
   cin >> b;

   //Calculate the decimal arithmetic sum of a and b and print the result
   int c1 = a + b;
   cout << "In decimal " << a << " + " << b << " is " << c1 << endl;

   //Compute the two's complement representations of a and b
   //Each integer must be represented in L-bits pattern
   //Also these two's complement representations must be returned as string data types
   string A = decimalToTwosComplementString(a, L);
   string B = decimalToTwosComplementString(b, L);

   //Print the two's complement representations of a and b
   cout << "The two's complement of " << a << " is\t " << A << endl;
   cout << "The two's complement of " << b << " is\t " << B << endl;

   //Compute the binary sum of the two's complement representations of a and b
   //The result must be returned as L-bit pattern string data type
   string C = twosComplementStringsAddition(A, B);

   //Print the two's complement representation binary sum
   cout << "The binary sum of " << A << " and " << B << " is " << C << endl;

   //Convert the two's complement representation binary sum to decimal and print
   int c2 = twosComplementStringToDecimal(C);
   cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;

   //Print some concluding results
   if (c1 == c2)
       cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
   else
   {
       cout << c1 << " is not equal to " << c2 << endl;
       cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made a mistake!" << endl;
   }
   system("Pause");
   return 0;
}

Sample Output Screenshots:


Related Solutions

Convert decimal +47 and +31 to binary, using the signed-2’s-complement representation and enough digits to accommodate...
Convert decimal +47 and +31 to binary, using the signed-2’s-complement representation and enough digits to accommodate the numbers. Then perform the binary equivalent of (+31)+(-47), (-31)+(+47), and (-31)+(-47). Convert the answers back to decimal and verify that they are correct.
(16%) Convert decimal +47 and +38 to binary, using the signed-2’s-complement representation and enough digits to...
(16%) Convert decimal +47 and +38 to binary, using the signed-2’s-complement representation and enough digits to accommodate the numbers, Then, perform the binary equivalent of (+47)+(-38) and (-47)+(-38) using addition. Convert the answers back to decimal and verify that they are correct.
Please convert decimal value -32760 into 2's complement binary value
Please convert decimal value -32760 into 2's complement binary value
Convert the following decimal numbers to binary (You may use as many binary digits as necessary)...
Convert the following decimal numbers to binary (You may use as many binary digits as necessary) 25610 246710 102310 2310 710 Convert the following binary numbers to hexadecimal 1101 0010 1101 1111 1100 0101 0010 1001 1000 0000 0000 0001 0001 1000 0000 0000 Given  bits, how many unsigned binary numbers can be represented? Given  bits, how many signed binary numbers can be represented using the two’s compliment method? In the two’s complement method, why is there one more negative number than...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know...
I have a decimal fraction: 76.234567x10^-14 I need to convert this number to binary. I know you can multiply the number by 2 constantly to get the binary number, but that will take forever to do, even converting it to hex with x16 takes a long time, is there any easier way to convert this? Show all steps and work please.
convert +38 and +17 to binary using the signed 2s complement representation and enough digits to...
convert +38 and +17 to binary using the signed 2s complement representation and enough digits to accomaodate the numbers. Then perform the binary equivalent of (-38) and +17
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of each of these lists to generate a total sum Print the lists, and the total sum of each value C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm. The input values are: 5 9 24 2 39 83 60 8...
(a) Convert the decimal numbers, 70 and -26 to binary in the signed 2’s complement system....
(a) Convert the decimal numbers, 70 and -26 to binary in the signed 2’s complement system. Make sure there are enough digits in the results to be able to perform arithmetic operations with these two numbers. (b) Perform in the signed 2’s complement system, (+70) + (-26) (c) Perform in the signed 2’s complement system, (-70) - (-26) (d) Perform in the signed 2’s complement system, (+70) + (+26)
Convert signed integer 0xACE9 to binary and decimal. Convert unsigned integer0xACE9 to binary and decimal.
Convert signed integer 0xACE9 to binary and decimal. Convert unsigned integer0xACE9 to binary and decimal.
First, Calculate the 1/3 in binary form using 8-digits. Then convert binary form back to decimal....
First, Calculate the 1/3 in binary form using 8-digits. Then convert binary form back to decimal. Why and what is the error in binary representation ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT