我正在尝试添加两个二进制数字。输出很好,除了最后一位。如代码下方的图片所示,未显示最后一位。有人知道这里有什么问题吗?
int main(){
char C[9]={'\0','\0','\0','\0','\0','\0','\0','\0','\0'};
char B[9]={'0','1','0','0','0','0','0','1','\0'};
char A[9]={'0','1','0','0','0','0','0','0','\0'};
char sum[9]={'\0','\0','\0','\0','\0','\0','\0','\0','\0'};
char carry='0';
for(int i = 7; i >= 0; i--) {
C[i] = (A[i] ^ B[i]) ^ carry;
carry = A[i] & B[i];
}
cout << A << endl << B << endl << C << endl; //output shown below
//for wrap-around carry
for(int i = 7; carry!=0; i--) {
sum[i] = C[i] ^ carry;
carry = C[i] & carry;
}
return 0;
}