-
Notifications
You must be signed in to change notification settings - Fork 13.3k
implement .trailing_ones(), .leading_ones() #55715
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,6 +329,27 @@ $EndFeature, " | |
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of leading ones in the binary representation of `self`. | ||
|
||
# Examples | ||
|
||
Basic usage: | ||
|
||
``` | ||
", $Feature, "let n = 0b10110011", stringify!($SelfT), "; | ||
|
||
assert_eq!(n.leading_ones(), 1);", | ||
$EndFeature, " | ||
```"), | ||
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not on the libs team but it might be a good idea to open an issue so you have a non-zero number you can refer to here :) |
||
#[rustc_const_unstable(feature = "const_int_ops")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just remove this; I think it's fairly certain that we'll want it to be |
||
#[inline] | ||
pub const fn leading_ones(self) -> u32 { | ||
(!self as $UnsignedT).leading_zeros() | ||
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of trailing zeros in the binary representation of `self`. | ||
|
||
|
@@ -350,6 +371,27 @@ $EndFeature, " | |
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of trailing ones in the binary representation of `self`. | ||
|
||
# Examples | ||
|
||
Basic usage: | ||
|
||
``` | ||
", $Feature, "let n = 0b10110011", stringify!($SelfT), "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tidy is saying that you need |
||
|
||
assert_eq!(n.trailing_ones(), 2);", | ||
$EndFeature, " | ||
```"), | ||
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")] | ||
#[rustc_const_unstable(feature = "const_int_ops")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, I'd remove this line. |
||
#[inline] | ||
pub const fn trailing_ones(self) -> u32 { | ||
(!self as $UnsignedT).trailing_zeros() | ||
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Shifts the bits to the left by a specified amount, `n`, | ||
wrapping the truncated bits to the end of the resulting integer. | ||
|
@@ -2260,6 +2302,26 @@ assert_eq!(n.leading_zeros(), 2);", $EndFeature, " | |
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of leading ones in the binary representation of `self`. | ||
|
||
# Examples | ||
|
||
Basic usage: | ||
|
||
``` | ||
", $Feature, "let n = 0b11001100", stringify!($SelfT), "; | ||
|
||
assert_eq!(n.leading_ones(), 2);", $EndFeature, " | ||
```"), | ||
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")] | ||
#[rustc_const_unstable(feature = "const_int_ops")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here... |
||
#[inline] | ||
pub const fn leading_ones(self) -> u32 { | ||
(!self).leading_zeros() | ||
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of trailing zeros in the binary representation | ||
of `self`. | ||
|
@@ -2281,6 +2343,27 @@ assert_eq!(n.trailing_zeros(), 3);", $EndFeature, " | |
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Returns the number of trailing ones in the binary representation | ||
of `self`. | ||
|
||
# Examples | ||
|
||
Basic usage: | ||
|
||
``` | ||
", $Feature, "let n = 0b0101011", stringify!($SelfT), "; | ||
|
||
assert_eq!(n.trailing_ones(), 2);", $EndFeature, " | ||
```"), | ||
#[unstable(feature = "leading_ones_trailing_ones", issue = "0")] | ||
#[rustc_const_unstable(feature = "const_int_ops")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here. |
||
#[inline] | ||
pub const fn trailing_ones(self) -> u32 { | ||
(!self).trailing_zeros() | ||
} | ||
} | ||
|
||
doc_comment! { | ||
concat!("Shifts the bits to the left by a specified amount, `n`, | ||
wrapping the truncated bits to the end of the resulting integer. | ||
|
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.
I don't think this will pass -- this will have no leading ones for any type size but
u8
.