-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Update wrapping_sh[lr] docs and examples
#149837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
rustbot has assigned @workingjubilee. Use |
fb36731 to
812137c
Compare
812137c to
ff43366
Compare
| #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")] | ||
| #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")] | ||
| #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")] | ||
| #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be nice to do some of these examples in binary (0b) or hex (0x), so one can see the bits move better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, actually, demonstrating the shift from 0b0101 to 0b1010 sounds nice. Don't have to do it for all of them, ofc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call; I added a couple more basic examples to show the bits before the extreme examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// then truncating as needed. The behaviour matches what shift instructions | ||
| /// do on many processors, and is what the `<<` operator does when overflow | ||
| /// checks are disabled, but numerically it's weird. Consider, instead, | ||
| /// using [`Self::unbounded_shl`] which has nicer behaviour. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, hm.
Is "nicer" quite right? "More numerical" behavior? "As an integer"? "More logical"? "Is more similar to the mul-by-pow-2 operation you were looking for"?
Inspired by #general > `Source` link for `core` items is often inscrutable @ 💬 I wanted to add some more examples of the actual wrapping as well as update the documentation to emphasize that the behaviour is unusual.
In particular, now that
unbounded_sh[lr]is stable, point people trying to avoid panics to that instead, since it behaves less weirdly.Rendered example:
