使用负数 [英] Working with negative numbers

查看:120
本文介绍了使用负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对负数系统的工作原理有一个大概的了解,但是如果有人愿意帮我的话,我会赞赏一些澄清。 />

让我们假设我们正在使用8位有符号整数,并且它

不包含填充。

首先,我意识到MSB被称为符号位,并且它b / b
表示该数字是正数还是负数(无论是否
使用哪个负数系统。


接下来的七位告诉我们价值。如果数字是正数,那么

七位代表国内相同的数字,其中

无符号整数代表数字。


但是如果数字是负数,那么有一些单独的方法可以将负位模式与其相应的正值相关联:


(1)符号幅度:位模式完全相同。

(2)一个补码:切换每一位。

(3) )两个补码:切换每一位,然后加1.

看来我们为每种格式得到以下范围:(它们是对的吗?)


(1)符号幅度:-127到+127

(2)一个补码:-128到+127

(3)两个'' s补码:-128到+127

看起来每个数字系统都有以下优点:


(1)符号幅度:


从负面到正面的高效转换。


(2)一个补充:


只有一个位模式为零。

负数范围内的一个额外单位。


(3)两个补码:


只有一个位模式为零。

一个额外的负数范围内的单位。

加上负数可以完全像

他们是正数。

是否还有其他优势/需要注意的缺点?


我要讨论的下一件事就是为特定值提供超过

一位模式的整体想法。 ..特别是零!


如果我们有一台使用符号幅度的机器,我们有两个单独的

变量,其值为零,他们有可能有不同的位模式吗?即:


var1 == 0000 0000

var2 == 1000 0000

机器如何处理这两个变量的比较?会不会是
解释:


if(var1 == var2)


as:


if(!(var1& 127 || var2& 127))


当人们回复时,我会问更多问题...... />
-


Frederick Gotham


I have a general idea about how negative number systems work, but I''d
appreciate some clarification if anyone would be willing to help me.

Let''s assume we''re working with an 8-Bit signed integer, and that it
contains no padding.

Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).

The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
unsigned integer would represent the number.

But if the number is negative, then there''s separate methods to correlate
the negative bit-pattern with its corresponding positive value:

(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One''s complement: Toggle each bit.
(3) Two''s complement: Toggle each bit, then add 1.
It seems we get the following ranges for each format: (Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One''s complement: -128 through +127
(3) Two''s complement: -128 through +127
It would seem that each number system has the following advantages:

(1) Sign-magnitude:

Efficient conversion from negative to positive.

(2) One''s complement:

There''s only one bit-pattern for zero.
One extra unit in the negative range.

(3) Two''s complement:

There''s only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Are there any other advantage/disadvantages to be aware of?

The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000
How does the machine handle comparison of these two variables? Would it
interpret:

if ( var1 == var2 )

as:

if ( !(var1 & 127 || var2 & 127) )

I''d ask more question as people reply...
--

Frederick Gotham

推荐答案

Frederick Gotham写道:
Frederick Gotham wrote:
似乎我们为每种格式得到以下范围:
(它们是对的吗?)

(1)符号幅度:-127到+127
(2)一个补码:-128到+127
(3)两个补码:-128到+127


是的,除了实现

允许SCHAR_MIN等于-127,

无论代表什么。


接下来我想要的东西讨论的是为特定值提供超过一个位模式的整个想法......特别是零!

如果我们有一台使用符号幅度的机器,
和我们有两个单独的
变量,它们保持零值,它们是否有可能具有不同的位模式?即:

var1 == 0000 0000
var2 == 1000 0000

机器如何处理这两个变量的比较?
It seems we get the following ranges for each format:
(Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One''s complement: -128 through +127
(3) Two''s complement: -128 through +127
Yes, except that implementations
are allowed to have SCHAR_MIN equal to -127,
regardless of representation.

The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude,
and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000

How does the machine handle comparison of these two variables?



机器有两种选择,

一个整数对象,表示负零,

比较等于零

或有陷阱代表。


-

pete



A machine has two choices,
an integer object with the representation for negative zero,
either compares equal to zero
or has a trap representation.

--
pete


pete已发布:

pete posted:

var1 == 0000 0000
var2 == 1000 0000

机器手柄比较这两个变量?
var1 == 0000 0000
var2 == 1000 0000

How does the machine handle comparison of these two variables?



一台机器有两个选择,一个整数对象,表示负零,
比较等于零<或者有一个陷阱表示。



A machine has two choices,
an integer object with the representation for negative zero,
either compares equal to zero
or has a trap representation.



很好地放。


这是否意味着你必须在有符号整数内有填充顺序

为负零和曲 OT;成为陷阱代表?我问的原因是:

如下:我记得最近阅读的内容是整数类型的价值

表示位不应该包含在陷阱中。 。

这让我觉得,如果你有一个陷阱表示,那么你必须至少有一个填充位。

-


Frederick Gotham


Nicely put.

Does that mean that you MUST have padding within a signed integer in order
for "negative zero" to be a trap representation? The reason I ask is as
follows: I recall reading something recently along the lines of "The value
representation bits of an integer type shall not be involved in trapping".
This makes me think that, if you''re going to have a trap representation,
then you must have at least one padding bit.
--

Frederick Gotham


Frederick Gotham schrieb:
Frederick Gotham schrieb:
我有一个将军关于负数系统如何工作的想法,但如果有人愿意帮助我,我会感激一些澄清。

我们假设我们正在使用8 -Bit有符号整数,并且它没有填充。

首先,我意识到MSB被称为符号位,并且它表示是否为数字是积极的还是消极的(无论使用哪种负数系统)。

接下来的七位告诉我们的价值。如果数字是正数,那么这七位代表国内相同的数字,其中


国内?

无符号整数将代表数字。

但如果数字是负数,那么有一些单独的方法可以将负位模式与其相应的正值相关联:
(1)符号幅度:位模式完全相同。
(2)一个补码:切换每一位。
(3)两个补码:切换每一位,然后添加1.


请注意,最后一条规则不包括

10000000

而规则假装无符号在1 <<<<<<<<<<<<<<< 8> -1的范围内的值的表示被视为''无符号值 -

(1<< 8) '' "确实如此。


请注意,C90没有规定这三个中的一个,所以你可以用b $ b来提出符号幅度 - 1。或符号加倒置订单

值位或者......

C99将您的选择限制为1s补码,2s补码和

符号幅度。

看来我们得到以下内容每种格式的范围:(它们是对的吗?)

(1)符号幅度:-127到+127
(2)一个补码:-128到+127


错了。重读上面的规则。一个补充封面

-127到127

(3)两个补码:-128到+127

看起来每个数字系统具有以下优点:

(1)符号量:

从负到正的有效转换。


负数和正数的简单乘法/除法。

对称范围。

(2)一个补码:

零只有一个位模式。


错误。 11111111符合您的定义。

负数范围内的一个额外单位。


错误。

优点:〜表示与一元减去相同。

对称范围。

(3)两个补码:

零只有一个位模式。
负数范围内的一个额外单位。
加上负数可以完全像他们是积极的那样表现。


经常通过溢出计算可以扩大范围。


缺点:

- >>算术移位意味着向下舍入

分割而不是真正的分裂。

- 不对称范围:你为一个额外的

单位支付的费用太多了。必须写-32767-1才能确保你用正确的类型到达正确的价值只是一点点太多了b / b
。这可能导致许多丑陋的检查,对于许多事情来说可能是很难的,因为值保留而不是

无符号保留。[*]

是否还有其他优点/缺点需要注意?


当然 - 取决于您的申请。请参阅上面的一些

他们。

我要讨论的下一件事就是为特定的一个比特模式提供更多的概念值......特别是零!

如果我们有一台使用符号幅度的机器,并且我们有两个单独的
变量保持零值,它们是否可能有不同的位模式?即:

var1 == 0000 0000
var2 == 1000 0000


是的。该实现可以将10000000(或1sC,11111111)

视为陷阱表示。或负零。

C99标准专门处理负零和负零。 (我太懒了~b $ b懒得查看,看看N1124.pdf;我也太懒了

找出你能在C90找到的东西)。

机器如何处理这两个变量的比较?它是否会解释:

if(var1 == var2)

如果:(!(var1& 127 | | var2& 127))
I have a general idea about how negative number systems work, but I''d
appreciate some clarification if anyone would be willing to help me.

Let''s assume we''re working with an 8-Bit signed integer, and that it
contains no padding.

Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).

The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
domestic?
unsigned integer would represent the number.

But if the number is negative, then there''s separate methods to correlate
the negative bit-pattern with its corresponding positive value:

(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One''s complement: Toggle each bit.
(3) Two''s complement: Toggle each bit, then add 1.
Note that the last rule does not cover
10000000
whereas the rule "pretend that unsigned representations of values
in the range 1<<7 to (1<<8)-1 are treated as ''unsigned value -
(1<<8)''" does.

Note that C90 does not prescribe one of these three, so you could
come up with "sign magnitude - 1" or "sign plus inverted order
value bits" or ...
C99 restricts your choice to 1s complement, 2s complement, and
sign-magnitude.
It seems we get the following ranges for each format: (Are they right?)

(1) Sign-magnitude: -127 through +127
(2) One''s complement: -128 through +127
Wrong. Reread your rule above. Ones complement covers
-127 to 127
(3) Two''s complement: -128 through +127
It would seem that each number system has the following advantages:

(1) Sign-magnitude:

Efficient conversion from negative to positive.
Easy multiplication/division of negative and positive numbers.
Symmetric range.

(2) One''s complement:

There''s only one bit-pattern for zero.
Wrong. 11111111 fits your definition.
One extra unit in the negative range.
Wrong.
Advantages: ~ means the same as unary minus.
Symmetric range.
(3) Two''s complement:

There''s only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Often "compute through overflow" possible to extend range.

Disadvantages:
- >> for arithmetic shift means "round down
division" instead of real division.
- asymmetric range: You pay simply too much for the "one extra
unit". Having to write -32767-1 in order to make sure that you
arrive at the right value with the right type simply is a
little bit too much. This can incur many ugly checks and can be
for many things as annoying as "value preserving instead of
unsigned preserving".[*]
Are there any other advantage/disadvantages to be aware of?
Sure -- depending on your application. See above for a few of
them.
The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.

If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:

var1 == 0000 0000
var2 == 1000 0000
Yes. The implementation can treat 10000000 (or, for 1sC, 11111111)
as "trap representation" or "negative zero".
The C99 standard specifically deals with "negative zero" (I am too
lazy to look it up, have a look at N1124.pdf; I am also too lazy
to find out what you can find in C90).
How does the machine handle comparison of these two variables? Would it
interpret:

if ( var1 == var2 )

as:

if ( !(var1 & 127 || var2 & 127) )




这是实施的问题,不是你的问题,所以你要走向

主题。

干杯

Michael

-

电子邮件:我的是/ at / gmx /点/地址。



This is the implementation''s problem, not yours, so you are going
toward off-topic.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于使用负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆