如何找到无符号幅度和2恭维的最小位数?

时间:2015-08-22 16:00:10

标签: binary unsigned bits twos-complement

我对于找到无符号幅度和2恭维的最小位数感到有些困惑。 到目前为止,这是我的理由: 例如,

a) 243 decimal
Since 2^8 = 256, Unsigned and 2's compliment would both need a minimum 8 bits.
b) -56 decimal
This is impossible for unsigned.
2^6 = 64. One more bit is needed to show it is negative, so minimum 7 bits.

我的推理是否正确?

1 个答案:

答案 0 :(得分:1)

需要"比特"对于无符号,只是最重要的位(+1,取决于MSB的定义),对于2的补码,你可以只是否定该值并减去1使其为正,然后为符号标志添加另一位

Exception in thread "main" com.github.axet.wget.info.ex.DownloadRetry: no video with required quality found
    at com.github.axet.vget.info.VGetParser.getVideo(VGetParser.java:38)
    at com.github.axet.vget.info.YouTubeParser.extract(YouTubeParser.java:388)
    at com.github.axet.vget.info.VGetParser.extract(VGetParser.java:15)
    at com.github.axet.vget.info.VideoInfo.extract(VideoInfo.java:100)
    at com.github.axet.vget.VGet.download(VGet.java:224)
    at com.github.axet.vget.VGet.download(VGet.java:68)
    at Test.main(Test.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

这基于你对问题的定义,至少是这样。对我而言,似乎+243需要9位用于2的补码,因为符号位的0仍然是相关的。