The OpenD Programming Language

subu

Subtract two unsigned integers, checking for overflow (aka borrow).

The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.

  1. uint subu(uint x, uint y, bool overflow)
    nothrow @safe @nogc pure pragma(inline, true)
    uint
    subu
    ()
    (
    uint x
    ,
    uint y
    ,
    ref bool overflow
    )
  2. ulong subu(ulong x, ulong y, bool overflow)
  3. ucent subu(ucent x, ucent y, bool overflow)

Parameters

x uint

left operand

y uint

right operand

overflow bool

set if an overflow occurs, is not affected otherwise

Return Value

Type: uint

the difference

Examples

bool overflow;
assert(subu(3, 2, overflow) == 1);
assert(!overflow);

assert(subu(uint.max, 1, overflow) == uint.max - 1);
assert(!overflow);

assert(subu(1, 1, overflow) == uint.min);
assert(!overflow);

assert(subu(0, 1, overflow) == uint.max);
assert(overflow);

overflow = false;
assert(subu(uint.max - 1, uint.max, overflow) == uint.max);
assert(overflow);

assert(subu(0, 0, overflow) == 0);
assert(overflow);                   // sticky
bool overflow;
assert(subu(3UL, 2UL, overflow) == 1);
assert(!overflow);

assert(subu(ulong.max, 1, overflow) == ulong.max - 1);
assert(!overflow);

assert(subu(1UL, 1UL, overflow) == ulong.min);
assert(!overflow);

assert(subu(0UL, 1UL, overflow) == ulong.max);
assert(overflow);

overflow = false;
assert(subu(ulong.max - 1, ulong.max, overflow) == ulong.max);
assert(overflow);

assert(subu(0UL, 0UL, overflow) == 0);
assert(overflow);                   // sticky

Meta