Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a program (in C++) that first asks the user to input a signed number, fo

ID: 3775753 • Letter: D

Question

Develop a program (in C++) that first asks the user to input a signed number, for example 48, then converts it to binary number by using Two's Complement regulations. Do the same for the second inputted number, e.g., -17. Then compute the summation of these two numbers, output the results in binary format first, then if there is an overflow, display error message; otherwise, convert the result to decimal value and show it on the screen, e.g., 48 + (-17) = 29. Note that there is no limit to the number of bits your program can handle. For convenience, your program can assume it is for eight bit binary numbers. Also assume all inputs are valid. You CANNOT use library functions in the header file to implement it.

Explanation / Answer

long dectobin(int);

int main()

{

int n1,bin_no1,n2,bin_no2,carry=1,onescomp[9],twoscomp[9],onescomp1[9],twoscomp1[9];

cout<<"Enter 1st number:";

cin>>n1;

bin_no1=dectobin(n1);

for(int i=0;i<9;i++)

{

if(bin_no1[i]=='1')

{

onescomp[i]='0';

}

else if(bin_no1[i]=='0')

{

onescomp[i]='1';

}

}

onescomp[9]='';

for(i=8;i>=0;i--)

{

if(onescomp[i]=='1' && carry==1)

{

twoscomp[i]='0';

}

else if(onescomp[i]=='0' && carry==1)

{

twoscomp[i]='1';

carry=0;

}

else

{

twoscomp[i]=onescomp[i];

}

}

twoscomp[9]='';

cout<<"Enter 2nd number:";

cin>>n2;

bin_no2=dectobin(n2);

for(int i=0;i<9;i++)

{

if(bin_no2[i]=='1')

{

onescomp1[i]='0';

}

else if(bin_no2[i]=='0')

{

onescomp1[i]='1';

}

}

onescomp1[9]='';

for(i=8;i>=0;i--)

{

if(onescomp1[i]=='1' && carry==1)

{

twoscomp1[i]='0';

}

else if(onescomp1[i]=='0' && carry==1)

{

twoscomp1[i]='1';

carry=0;

}

else

{

twoscomp1[i]=onescomp1[i];

}

}

twoscomp1[9]='';

for (int j=0;j<8;j++)

{

sum[i]=((twoscomp[i]^twoscomp1[i]^carry);

carry=((twoscomp[i]&twoscomp1[i])|(twoscomp[i]&carry)|(twoscomp1[i]&carry);

}

cout<<carry;

return 0;

}

long dectobin(int a)

{

long bin=0;int rem,i=1,count=1;

while(a!=0)

{

rem=a%2;

bin ++ rem*i;

i*=10;

}

return bin;

}

Thank you for asking CHEGG