Search
View source code
Display the source code in core/checkedint.d from which this page was generated on github.
Report a bug
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

# Function `core.checkedint.subu`

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

``` uint subu (   uint x,   uint y,   ref bool overflow ); ulong subu (   ulong x,   ulong y,   ref bool overflow ); ```

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

## Parameters

NameDescription
x left operand
y right operand
overflow set if an overflow occurs, is not affected otherwise

the difference

## Example

``````bool overflow;
writeln(subu(3, 2, overflow)); // 1
assert(!overflow);

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

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

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

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

writeln(subu(0, 0, overflow)); // 0
assert(overflow);                   // sticky
``````

## Example

``````bool overflow;
writeln(subu(3UL, 2UL, overflow)); // 1
assert(!overflow);

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

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

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

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

writeln(subu(0UL, 0UL, overflow)); // 0
assert(overflow);                   // sticky
``````

Walter Bright