ITADN

Wrapping shift confusion

#1151Openandrewwhitehead 创建于 2026-01-23
A
andrewwhiteheadcommented
Currently, `num_traits::WrappingShl` and `WrappingShr` are implemented for numeric types. So are the `ShlVartime`/`ShrVartime` traits which also have corresponding wrapping shift methods (but vartime). The method description in `num_traits` matches the one for `u32::wrapping_shl` and related standard-library methods, which "masks off" the high bits of the shift. This means that `N.wrapping_shl(N::BITS) == N.wrapping_shl(0) == N`. This does not correspond with the behaviour in the library, which returns `N::ZERO` when the shift exceeds the maximum size (for types other than Limb). The standard library calls this `unbounded_shl`/`unbounded_shr` (new as of Rust 1.87).
6 条评论