Minimum bits required for two's complement number representation

旧街凉风 提交于 2019-12-13 17:19:47

问题


I need to find out that how can we represent -1 and -3 in minimum number of bits in Two's complement number system. I calculated the answer 1 and 111 but the answers seem to be incorrect. I would be very thankful if I can get some help. Thanks


回答1:


Here's the formula you're probably already familiar with: N' = 2^n - N. Where n is number of bits, N' is the decimal representation of -N's complement, and N is the cardinal number. For example, short int x = -6 is going to be N' = 2^8 - 6 = 250 when converted to unsigned short int.

Now, with this formula, you can get n = log(N+N') (log of base 2).

Edit:

I was more focused on just the number of bits. Now I've re-read your question... Let me give you an answer: You need at least two bits to represent 3 and you need that one extra bit to represent signness, which means you need at least 3 bits to represent -3. Same goes for 1. Having that in mind, [011] = 3, take the complement of one (inverting bits) => [100] and add 1 => [101] = -3. As for the -1, you do the same. [01] = 1, invert the bits => [10] => add one => [11] = -1.

That's it, I think...




回答2:


-1 can be represented by 1 and -3 can be represented by 101 (-4 + 1).

111 is equal to decimal -1 (-4 + 2 + 1).



来源:https://stackoverflow.com/questions/8204075/minimum-bits-required-for-twos-complement-number-representation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!