The Rust Programming Language Forum
How to make an integer with N bits set without overflow
s3bk
August 2, 2021, 2:22pm
3
pub fn setbits(x: u8) -> u32 { u32::MAX >> (32 - x) }
2 Likes
show post in topic
Related topics
Topic
Replies
Views
Activity
Apparent arithmetic bug
help
22
1252
July 11, 2020
Bitshift and overflowing behavior
help
3
2933
January 12, 2023
Why do the non-panicking variants of left bit-shift work the way they do?
help
12
2731
July 1, 2023
Disable overflow check
15
1929
October 9, 2023
>> operator panics in debug build, works in release build
help
15
2287
January 12, 2023