有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 1的补码到2的补码转换

我的代码是一个将十进制转换为二进制的类。它将显示二进制、一的补码和二的补码。到目前为止,我已经算出了一的补码,但在找到两个补码的解决方案时遇到了问题。我的计划是使用1补码的输出作为2补码方法的输入,并在那里进行转换。这是正确的方法吗

//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


} */
}

共 (0) 个答案