How to represent a negative number with a fraction in 2's complement?

試著忘記壹切 提交于 2019-12-18 11:45:22

问题


So I want to represent the number -12.5. So 12.5 equals to:

001100.100

If I don't calculate the fraction then it's simple, -12 is:

110100

But what is -12.5? is it 110100.100? How can I calculate this negative fraction?


回答1:


With decimal number systems, each number position (or column) represents (reading a number from right to left): units (which is 10^0), tens (i.e. 10^1),hundreds (i.e. 10^2), etc.

With unsigned binary numbers, the base is 2, thus each position becomes (again, reading from right to left): 1 (i.e. 2^0) ,2 (i.e. 2^1), 4 (i.e. 2^2), etc.

For example

2^2 (4), 2^1 (2), 2^0 (1).

In signed twos-complement the most significant bit (MSB) becomes negative. Therefore it represent the number sign: '1' for a negative number and '0' for a positive number.

For a three bit number the rows would hold these values:

-4, 2, 1
 0  0  1 => 1
 1  0  0 => -4
 1  0  1 => -4 + 1 = -3

The value of the bits held by a fixed-point (fractional) system is unchanged. Column values follow the same pattern as before, base (2) to a power, but with power going negative:

2^2 (4), 2^1 (2), 2^0 (1) . 2^-1 (0.5), 2^-2 (0.25), 2^-3 (0.125)

-1 will always be 111.000
-0.5 add 0.5 to it: 111.100

In your case 110100.10 is equal to -32+16+4+0.5 = -11.5. What you did was create -12 then add 0.5 rather than subtract 0.5.

What you actually want is -32+16+2+1+0.5 = -12.5 = 110011.1




回答2:


you can double the number again and again until it's negative integer or reaches a defined limit and then set the decimal point correspondingly.

-25 is 11100111, so -12.5 is 1110011.1




回答3:


So;U want to represent -12.5 in 2's complement representation

12.5:->> 01100.1

2's complement of (01100.1):->>10011.1

verify the ans by checking the weighted code property of 2's complement representation(MSB weight is -ve). we will get -16+3+.5=-12.5



来源:https://stackoverflow.com/questions/15463491/how-to-represent-a-negative-number-with-a-fraction-in-2s-complement

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