Answer to Question #22175 in C++ for rodea embradura
2013-01-15T10:23:26-05:00
create a c++ program that performs arithmetic binary addition.
1
2013-01-17T09:23:46-0500
#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 !
Learn more about our help with Assignments:
C++
Comments
Leave a comment