64 638
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #22175 in C++ for rodea embradura

Question #22175
create a c++ program that performs arithmetic binary addition.
Expert's answer

#include <iostream>
#include <string>
using namespace std;
string badd(string, string);
int main()
{
string left,right;
cout<<"Enter first digit: ";
cin>>left;
cout<<"Enter second digit: ";
cin>>right;
string res=(badd(left,right));
cout<<res<<endl;
getchar();
return 0;
}
string badd(string left, string right)
{
int l=0,r=0;
for(int i=left.length()-1;i>=0;--i)
{
if(left[i]=='1')
l+=(1<<(left.length()-1-i));
}
for(int i=right.length()-1;i>=0;--i)
{
if(right[i]=='1')
r+=(1<<(right.length()-1-i));
}
unsigned int res=l+r;
int i=31;
while((res>>i)==0)
--i;
string sres;
while(i>=0)
{
if((res>>i)&0x1)
sres.push_back('1');
else
sres.push_back('0');
--i;
}
return sres;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions