Java的二进制补码为整 [英] Java two's complement binary to integer
本文介绍了Java的二进制补码为整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,转换为十进制二进制与 Integer.toBinaryString(355)= 0000000101100011
和
Integer.toBinaryString(-355)= 1111111010011101
(其中我把32位结果的低16位)。
I know that converting a decimal to binary with Integer.toBinaryString(355) = 0000000101100011
and
Integer.toBinaryString(-355) = 1111111010011101
(where I take the lower 16 bits of the 32 bit result).
我想这样做的是另一种方式,并采取一个16位二进制补码二进制补码串并转换为十进制。
What I would like to do is the other way and take a 16-bit twos's complement binary string and to convert to decimal.
即
0000000000110010 = 50
1111111111001110 = -50
而不是 1111111111001110 = 65486
我将如何做到这一点?
推荐答案
您需要阅读的结果为短
。
short res = (short)Integer.parseInt("1111111111001110", 2);
System.out.println(res);
这版画 -50
。
这篇关于Java的二进制补码为整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文