Home > Software design >  Can integer division yield a different result when applied on unsigned operands vs signed operands?
Can integer division yield a different result when applied on unsigned operands vs signed operands?

Time:10-06

I'm asking, of course, in terms of the actual "bit data" returned.

In other words, can the following function ever return false:

bool func(uint x, uint y)
{
    return x / y == (uint)((int)x / (int)y);
}

?

CodePudding user response:

Absolutely it can. 4294967295 / 2 == 0x7fffffff, but -1 / 2 == 0.

The big value is of type uint32_t where all bits are set, and -1 is also a 32-bit value int32_t where all the bits are set. When the top bit of the parameters is set, you should expect different results. The exception is a / a == 1, for both signed and unsigned divisions, when a != 0.

The thing is that the compiler knows to emit a different instruction for the CPU, when the arithmetic is between signed vs unsigned operands.

  • Related