I'm having trouble adding 2 binary numbers in C++, instead of having the user input the binary numbers into the array (which I've gotten down) I shortened it to use two predetermined numbers 1010 + 0110, which should total to 10000, but the result I'm getting is 0012FEB0. Could anyone tell me where I'm going wrong with this?
Thanks in advance for any help.
Code:#include <iostream> using namespace std; int b2_addition (int a[], int b[]); int main (void) {int a[4]={1,0,1,0}, b[4]={0,1,1,0}, c[8]; b2_addition(a, b); cout<<c; return(0); } int b2_addition (int a[], int b[]) {int c[8], n_bits=4, carry=0; for(int i=0;i<n_bits;i++) {int carry=0; c[i]=a[i]+b[i]+carry; if(c[i]==3) {c[i]=1; carry=1;} if(c[i]==2) {c[i]=0; carry=1;} if(c[i]==1) {c[i]=1; carry=0;} if(c[i]==0) {c[i]=0; carry=0;} } if(carry==1) {n_bits++; c[n_bits]=1;} return(c[8]); }



LinkBack URL
About LinkBacks


