简体   繁体   English

如何用2的补码中的分数表示负数?

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

So I want to represent the number -12.5 . 所以我想代表-12.5的数字。 So 12.5 equals to: 所以12.5等于:

001100.100

If I don't calculate the fraction then it's simple, -12 is: 如果我不计算分数那么它很简单, -12是:

110100

But what is -12.5? 但是什么是-12.5? is it 110100.100 ? 110100.100 How can I calculate this negative fraction? 我该如何计算这个负分数?

With decimal number systems, each number position (or column) represents (reading a number from right to left): units (which is 10^0), tens (ie 10^1),hundreds (ie 10^2), etc. 对于十进制数字系统,每个数字位置(或列)表示(从右到左读取数字):单位(10 ^ 0),十(即10 ^ 1),数百(即10 ^ 2)等。

With unsigned binary numbers, the base is 2, thus each position becomes (again, reading from right to left): 1 (ie 2^0) ,2 (ie 2^1), 4 (ie 2^2), etc. 对于无符号二进制数,基数为2,因此每个位置变为(再次,从右到左读取):1(即2 ^ 0),2(即2 ^ 1),4(即2 ^ 2)等。

For example 例如

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

In signed twos-complement the most significant bit (MSB) becomes negative. 在带符号的二进制补码中,最高有效位(MSB)变为负数。 Therefore it represent the number sign: '1' for a negative number and '0' for a positive number. 因此它代表数字符号:负数为“1”,正数为“0”。

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^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 -1将始终为111.000
-0.5 add 0.5 to it: 111.100 -0.5加0.5: 111.100

In your case 110100.10 is equal to -32+16+4+0.5 = -11.5. 在你的情况下, 110100.10等于-32 + 16 + 4 + 0.5 = -11.5。 What you did was create -12 then add 0.5 rather than subtract 0.5. 你做的是创建-12然后添加0.5而不是减去0.5。

What you actually want is -32+16+2+1+0.5 = -12.5 = 110011.1 你真正想要的是-32 + 16 + 2 + 1 + 0.5 = -12.5 = 110011.1

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 -25是11100111,所以-12.5是1110011.1

So;U want to represent -12.5 in 2's complement representation 所以;你想在2的补码表示中表示-12.5

12.5:->> 01100.1 12.5: - >> 01100.1

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

verify the ans by checking the weighted code property of 2's complement representation(MSB weight is -ve). 通过检查2的补码表示的加权代码属性来验证ans(MSB权重是-ve)。 we will get -16+3+.5=-12.5 我们得到-16 + 3 + .5 = -12.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM