我的代码是将十进制转换为二进制的类。它将显示二进制、一个补码和二进制补码。到目前为止,我已经弄清楚了一个补码,但是在找到一个二进制补码的解决方案时遇到了问题。我的计划是使用一个补码的输出作为二进制补码方法的输入并在那里转换。这是正确的方法吗?
//This method will convert binary result to ones complement
public String do1sComp(){
if (binaryValue.length() == 0)
doBinary();
//Ones Complement
char[] digits = binaryValue.toCharArray();
for (char digit : digits){
if(digit == '0'){
onesComp += "1";
}
else
onesComp += "0";
}
return onesComp;
}
/* This was my attempt to start the method
public String do2sComp(){
if (onesComp.length() == 0){
if (binaryValue.length() == 0)
doBinary();
do1sComp();
}
//Twos Complement
} */
}