hello guys~
I'm asked to make a program that will add two 32 bit binary numbers. I don't have any idea on what to do so please give me some tips and ideas.
This is what I've done so far and I still can't get the correct answer when I add two binary numbers. Please tell me what to do.
Code:# include<stdio.h> #include <string.h> char x[32]; char y[32]; char z[32]; int a; int carry=0; main() { printf("Please input the first binary number.\n"); scanf("%s", x); printf("Please input the second binary number.\n"); scanf("%s", y); for(a=0;a<=31;a++){ if (x[a]=='0' && y[a]=='0' && carry==0){ z[a]='0'; carry=0;} if (x[a]=='1' && y[a]=='0' && carry==0){ z[a]='1'; carry=0;} if (x[a]=='0' && y[a]=='1' && carry==0){ z[a]='1'; carry=0;} if (x[a]=='1' && y[a]=='1' && carry==0){ z[a]='0'; carry=1;} if (x[a]=='1' && y[a]=='1' && carry==1){ z[a]='1'; carry=1;} if (x[a]=='0' && y[a]=='0' && carry==1){ z[a]='1'; carry=0;} if (x[a]=='0' && y[a]=='1' && carry==1){ z[a]='0'; carry=1;} if (x[a]=='1' && y[a]=='0' && carry==1){ z[a]='0'; carry=1;} } printf("\nThe answer is "); for(a=31;a>=0;a--) printf("%c", z[a]); }



3Likes
LinkBack URL
About LinkBacks



