本文介紹了Java二進(jìn)制補碼到整數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我知道用 Integer.toBinaryString(355) = 0000000101100011
將十進(jìn)制轉(zhuǎn)換為二進(jìn)制Integer.toBinaryString(-355) = 1111111010011101
(我取 32 位結(jié)果的低 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 位二進(jìn)制補碼二進(jìn)制字符串并轉(zhuǎn)換為十進(jìn)制.
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
我該怎么做?
推薦答案
需要將結(jié)果讀入short
.
short res = (short)Integer.parseInt("1111111111001110", 2);
System.out.println(res);
這個打印-50
.
這篇關(guān)于Java二進(jìn)制補碼到整數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!