Wrapping
trait Wrapping<S> Import
Source: use arith::{Wrapping}; src/arith.hy Integer arithmetic that wraps around modulo 2^64 instead of panicking.
Useful for hashes and checksums. Implemented for int.
Required methods#
| Name | Description |
|---|---|
wrapping_add | a + b modulo 2^64. |
wrapping_sub | a - b modulo 2^64. |
wrapping_mul | a * b modulo 2^64. |
wrapping_neg | -a modulo 2^64 (INT_MIN stays INT_MIN). |
wrapping_pow | a ** exp modulo 2^64. |
wrapping_shl | a << (n & 63): the count is taken modulo 64. |
wrapping_shr | a >> (n & 63): the count is taken modulo 64. |
Examples#
use arith::{Wrapping, INT_MAX, INT_MIN};
use tests::assert;
assert!(INT_MAX.wrapping_add(1) == INT_MIN);
assert!(1.wrapping_shl(65) == 2);
See also#
intChecked: Integer arithmetic that returnsNoneinstead of panicking when the result does not fit.Saturating: Integer arithmetic that clamps toINT_MIN/INT_MAXinstead of panicking.Overflowing: Integer arithmetic that returns the wrapped result together with whether it wrapped.