这是我正在测试的方法
public static char[] convertDecimalTo2sComp(int decimal) {
char [] bits = new char [16];
double temp;
int tempInt;
String bitStr = " ";
for(int i = bits.length-1; i>=0; i--) {
//negative input
if(decimal <1) {
temp = Math.pow(2,16); //add 2^number of bits,then convert to binary
decimal += (int)temp;
}
//non-negative input
tempInt = decimal%2;
bitStr = tempInt + bitStr;
decimal/=2;
}
bits = bitStr.toCharArray();
return bits;
}
这就是我得到的错误:int类型的文字1111111111111000超出范围,在JUnit中来自
@Test
public void testNegativeDecimal() {
int data = -8;
assertEquals(1111111111111000,Convert.convertDecimalTo2sComp(data)); }
我该如何解决?