问题描述
我有一个作业来计算一些 8 位二进制数的校验和。 我已经走了很长一段路,但由于某种原因,它给了我错误的结果。例如,如果 x 是:10101010 并且 y 是:01101111,则校验和将为 11100110。但我的代码给出了校验和 01101110(反转所有)。 请注意总和的反转发生在不同的、已经创建的方法中。这意味着我的方法返回 10010001,但应该返回 00011001。
我哪里出错了?
八位组方法已经完成。
int[] x = new int[8];
Octet(String s){
if (s.length() != 8) {
System.out.println("Too few or too many characters");
return;
}
for (int i = 0; i < 8; i++) {
if (s.charAt(i) == '1') {
x[7 - i] = 1;
}
else {
x[7 - i] = 0;
}
}
}
Octet sum(Octet y) {
Octet result = new Octet("00000000");
int carry = 0;
for(int i = 0; i < 8; i++) {
result.x[i] = x[i] ^ y.x[i] + carry;
carry = x[i] & y.x[i];
}
if(carry == 1) {
for(int i = 0; i < 8 && carry == 0; i++) {
result.x[i] = result.x[i] ^ carry;
carry = result.x[i] & carry;
}
}
return result;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)