문제

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

도움이 되었습니까?

해결책

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

다른 팁

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top