Open tower120 opened 11 months ago
For most such things, it's just because nobody needed it yet.
However, note that shifting is a bit weird compared to other checked/overflowing/wrapping ops, because overflowing_shl
and overflowing_shr
will overflow the shift value, not the result. Is that what you're actually looking for?
I am aware of overflowing_shl
behavior in Rust. I personally need behavior, where shifting left too far results in zero (like in old x86 shl
instructions).
And the thing is, this:
let (res, ok) = 1usize.overflowing_shl(n);
res * (!ok as usize)
is faster then this:
1usize.checked_shl(n).unwrap_or(0)
https://rust.godbolt.org/z/3a6q5Wazf
UPDATE: there were error in example
I have need in
overflowing_shl
. Is there a reason, why there is no trait for it?P.S. Well, actually I need something like
saturating_shl
, but since there is no such thing in Rust,overflowing_shl
is the closest one.