是无符号整数减法定义的行为? [英] Is unsigned integer subtraction defined behavior?

查看:551
本文介绍了是无符号整数减法定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的code从别人谁似乎相信有一个来自同一类型的另一个整数减去一个无符号整数时,其结果将是负面的一个问题。这样code像这将是即使它发生在大多数体系结构工作不正确。

  unsigned int类型以铁蛋白;为了= getcounter();
而(1){
    TF = getcounter();
    如果((TF-要)> = TIME_LIMIT){
        打破;
    }
}

这是从C标准我能找到的只是含糊相关报价。


  

在无符号运算绝不能溢流,因为一个计算
  结果是不能重新由所得的无符号整数psented $ P $
  类型被减小模数比最大较大者的数目
  值可以是结果类型psented重新$ P $。


我想人们可以采取引用意味着当右操作数是大的动作被调整为在模数截数的上下文有意义

为0x0000 - 0x0001的== 0X 1 0000 - 0×0001 == 0xFFFF的

而不是使用依赖于实现签约语义:

为0x0000 - 0x0001的==(符号)(0 + -1)==(0xFFFF,但同时也0xFFFE或在0x8001)

这还是什么际pretation是正确的?是它在所有定义的?


解决方案

减法产生的无符号类型负数的结果是明确的:


  

      
  1. [...]在无符号操作数可以永远不会溢出的一种计算,
      因为不能再通过将得到的无符号整型psented $ P $结果为是
      减小模数比,可以是在一个最大值大的数目
      再$ P $的结果类型psented。
      (ISO / IEC 9899:1999(E)§6.2.5/ 9)

  2.   

正如你所看到的,(无符号)0 - (无符号)1 等于-1模UINT_MAX + 1,或者换句话说,UINT_MAX

请注意,虽然它说:在无符号操作数计算可以永远不会溢出,这可能会导致您认为这仅适用于超出上限,这是psented作为的动机的刑罚的实际约束力的部分:不能再通过所产生的无符号整型psented $ p $结果是
减小模数比,可以是在一个最大值大的数目
再通过将得到的式psented。$ P $这个短语并不限于上界类型的溢出,并同样适用于值太低,可以重新presented

I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to work on most architectures.

unsigned int To, Tf;

To = getcounter();
while (1) {
    Tf = getcounter();
    if ((Tf-To) >= TIME_LIMIT) {
        break;
    } 
}

This is the only vaguely relevant quote from the C standard I could find.

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

I suppose one could take that quote to mean that when the right operand is larger the operation is adjusted to be meaningful in the context of modulo truncated numbers.

i.e.

0x0000 - 0x0001 == 0x 1 0000 - 0x0001 == 0xFFFF

as opposed to using the implementation dependent signed semantics:

0x0000 - 0x0001 == (unsigned)(0 + -1) == (0xFFFF but also 0xFFFE or 0x8001)

Which or what interpretation is right? Is it defined at all?

解决方案

The result of a subtraction generating a negative number in an unsigned type is well-defined:

  1. [...] A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. (ISO/IEC 9899:1999 (E) §6.2.5/9)

As you can see, (unsigned)0 - (unsigned)1 equals -1 modulo UINT_MAX+1, or in other words, UINT_MAX.

Note that although it does say "A computation involving unsigned operands can never overflow", which might lead you to believe that it applies only for exceeding the upper limit, this is presented as a motivation for the actual binding part of the sentence: "a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type." This phrase is not restricted to overflow of the upper bound of the type, and applies equally to values too low to be represented.

这篇关于是无符号整数减法定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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