如何将二进制补码二进制字符串转换为负十进制数?
how to convert twos complement binary string to negative decimal number?
我正在尝试寻找一种 quick/easy 方法将二进制补码二进制字符串转换为负十进制数。我尝试使用 this question 中提供的方法,但它不起作用。这是我正在尝试的代码 运行:
short res = (short)Integer.parseInt("1001", 2);
System.out.println(res);
当我运行这段代码结果是9。
我错过了什么吗?
我做错了什么?
When i run this code the result is 9.
理应如此。
Am i missing something? What am i doing wrong?
您的代码与您引用的答案之间的区别在于输入中的位数。 "Two's complement" 在没有指定宽度的情况下定义不明确。您复制的答案是 16 位二进制补码,因为 Java short
是 16 位宽。如果您想要 4 位二进制补码,则没有相应的 Java 数据类型,因此您将无法采用相同的快捷方式。
在 Two's Complement algorithm, 之后我写了以下内容:
public static int getTwosComplement(String binaryInt) {
//Check if the number is negative.
//We know it's negative if it starts with a 1
if (binaryInt.charAt(0) == '1') {
//Call our invert digits method
String invertedInt = invertDigits(binaryInt);
//Change this to decimal format.
int decimalValue = Integer.parseInt(invertedInt, 2);
//Add 1 to the curernt decimal and multiply it by -1
//because we know it's a negative number
decimalValue = (decimalValue + 1) * -1;
//return the final result
return decimalValue;
} else {
//Else we know it's a positive number, so just convert
//the number to decimal base.
return Integer.parseInt(binaryInt, 2);
}
}
public static String invertDigits(String binaryInt) {
String result = binaryInt;
result = result.replace("0", " "); //temp replace 0s
result = result.replace("1", "0"); //replace 1s with 0s
result = result.replace(" ", "1"); //put the 1s back in
return result;
}
以下是一些示例运行:
run:
Two's Complement of: 1000: -8
Two's Complement of: 1001: -7
Two's Complement of: 1010: -6
Two's Complement of: 0000: 0
Two's Complement of: 0001: 1
Two's Complement of: 0111: 7
我正在尝试寻找一种 quick/easy 方法将二进制补码二进制字符串转换为负十进制数。我尝试使用 this question 中提供的方法,但它不起作用。这是我正在尝试的代码 运行:
short res = (short)Integer.parseInt("1001", 2);
System.out.println(res);
当我运行这段代码结果是9。 我错过了什么吗? 我做错了什么?
When i run this code the result is 9.
理应如此。
Am i missing something? What am i doing wrong?
您的代码与您引用的答案之间的区别在于输入中的位数。 "Two's complement" 在没有指定宽度的情况下定义不明确。您复制的答案是 16 位二进制补码,因为 Java short
是 16 位宽。如果您想要 4 位二进制补码,则没有相应的 Java 数据类型,因此您将无法采用相同的快捷方式。
在 Two's Complement algorithm, 之后我写了以下内容:
public static int getTwosComplement(String binaryInt) {
//Check if the number is negative.
//We know it's negative if it starts with a 1
if (binaryInt.charAt(0) == '1') {
//Call our invert digits method
String invertedInt = invertDigits(binaryInt);
//Change this to decimal format.
int decimalValue = Integer.parseInt(invertedInt, 2);
//Add 1 to the curernt decimal and multiply it by -1
//because we know it's a negative number
decimalValue = (decimalValue + 1) * -1;
//return the final result
return decimalValue;
} else {
//Else we know it's a positive number, so just convert
//the number to decimal base.
return Integer.parseInt(binaryInt, 2);
}
}
public static String invertDigits(String binaryInt) {
String result = binaryInt;
result = result.replace("0", " "); //temp replace 0s
result = result.replace("1", "0"); //replace 1s with 0s
result = result.replace(" ", "1"); //put the 1s back in
return result;
}
以下是一些示例运行:
run:
Two's Complement of: 1000: -8
Two's Complement of: 1001: -7
Two's Complement of: 1010: -6
Two's Complement of: 0000: 0
Two's Complement of: 0001: 1
Two's Complement of: 0111: 7