Why int plus uint returns uint?

前端 未结 3 1359
生来不讨喜
生来不讨喜 2020-12-20 13:14

int plus unsigned int returns an unsigned int. Should it be so?

Consider this code:

#include 
#include 

        
相关标签:
3条回答
  • 2020-12-20 13:31

    Unsigned integer types mostly behave as members of a wrapping abstract algebraic ring of values which are equivalent mod 2^N; one might view an N-bit unsigned integer not as representing a particular integer, but rather the set of all integers with a particular value in the bottom N bits. For example, if one adds together two binary numbers whose last 4 digits are ...1001 and ...0101, the result will be ...1110. If one adds ...1111 and ...0001, the result will be ...0000; if one subtracts ...0001 from ...0000 the result will be ...1111. Note that concepts of overflow or underflow don't really mean anything, since the upper-bit values of the operands are unknown and the upper-bit values of the result are of no interest. Note also that adding a signed integer whose upper bits are known to one whose upper bits are "don't know/don't care" should yield a number whose upper bits are "don't know/don't care" (which is what unsigned integer types mostly behave as).

    The only places where unsigned integer types fail to behave as members of a wrapping algebraic ring is when they participate in comparisons, are used in numerical division (which implies comparisons), or are promoted to other types. If the only way to convert an unsigned integer type to something larger was to use an operator or function for that purpose, the use of such an operator or function could make clear that it was making assumptions about the upper bits (e.g. turning "some number whose lower bits are ...00010110" into "the number whose lower bits are ...00010110 and whose upper bits are all zeroes). Unfortunately, C doesn't do that. Adding a signed value to an unsigned value of equal size yields a like-size unsigned value (which makes sense with the interpretation of unsigned values above), but adding a larger signed integer to an unsigned type will cause the compiler to silently assume that all upper bits of the latter are zeroes. This behavior can be especially vexing in cases where, depending upon a compilers' promotion rules, some compilers may deem two expressions as having the same size while others may view them as different sizes.

    0 讨论(0)
  • 2020-12-20 13:32

    If by "should it be" you mean "does my compiler behave according to the standard": yes.

    C++2003: Clause 5, paragraph 9:

    Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

    • blah
    • Otherwise, blah,
    • Otherise, blah, ...
    • Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

    If by "should it be" you mean "would the world be a better place if it didn't": I'm not competent to answer that.

    0 讨论(0)
  • 2020-12-20 13:43

    It is likely that the behavior stems from the logic behind pointer types (memory location, e.g. std::size_t) plus a memory location difference (std::ptrdiff_t) is also a memory location.

    In other words, std::size_t = std::size_t + std::ptrdiff_t.

    When this logic is translated to underlaying types this means, unsigned long = unsigned long + long, or unsigned = unsigned + int.

    The "other" explanation from @supercat is also possibly correct.

    What is clear is that unsigned integer were not designed or should not be interpreted to be mathematical positive numbers, no even in principle. See https://www.youtube.com/watch?v=wvtFGa6XJDU

    0 讨论(0)
提交回复
热议问题