Apparent arithmetic bug

As @mbrubeck pointed out, the real reason that the default behavior on shift is to mask the shift amount by the size of the shifted item is that most modern hardware uses a barrel shifter that implements shifts in precisely that way. Thus the default implementation is the hardware-efficient one, whereas the behavior you desire requires extra code, and often extra execution time, in all cases of a non-constant shift distance.

3 Likes

It is expected behavior. From the primary reference for the Rust programming language:

6 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.