Two's Complement of a number and its negative number difference?

只谈情不闲聊 提交于 2019-12-11 02:59:12

问题


So i was told that two's complement is usually to used to find complement of a number and I used it only to complement positive numbers (i.e positve --> negative conversion) however I just got an example in book which asks me the following :

Express 23, -23, and -9 in 8-bit binary two’s Complement form

now what does that mean? 23 means -23 in binary and -23 means 23 ?

sorta confused over there


回答1:


2's complement is used to represent negative numbers, which in turn, can be used to do subtraction.

23 = 00010111b

To get -23 (2's complement of 23), flip all the bits, and add 1:

 11101000b + 1
=11101001b (-23)

-9 is the 2's complement of 9. 9 is

00001001b

So -9 is

11110111b (Flipping and add +1)

See also here




回答2:


The representation of positive numbers in 2's complement is same as the unsigned representation. Things start to change when negative representation comes into play. So, in general, for given w bits, the numbers that can be represented in 2's complement are -2^(w-1)-1 to 2^(w-1) with the w bit being the signed bit. So since you have 8 bits you can represent numbers from -128 to 127. The 8th bit will be the signed bit, with 0 being positive and 1 representing negative. For any positive representation of a number in binary 2's complement N the negative counterpart -N is pretty simple to find, just invert the bits, and add one. Example: 7 in 2's complement is 0111, inverting these bits gives: 1000, adding one gives 1001, which is -7 in 2's complement! Hope this helps!



来源:https://stackoverflow.com/questions/22895770/twos-complement-of-a-number-and-its-negative-number-difference

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