Skip to content

Commit 6a7a69b

Browse files
author
Thom Chiovoloni
committed
Add {leading,trailing}_ones to primitive int types
1 parent 3ebcfa1 commit 6a7a69b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/libcore/num/mod.rs

+83
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,48 @@ $EndFeature, "
393393
}
394394
}
395395

396+
doc_comment! {
397+
concat!("Returns the number of leading ones in the binary representation of `self`.
398+
399+
# Examples
400+
401+
Basic usage:
402+
403+
```
404+
", $Feature, "#![feature(leading_trailing_ones)]
405+
let n = -1", stringify!($SelfT), ";
406+
407+
assert_eq!(n.leading_ones(), ", stringify!($BITS), ");",
408+
$EndFeature, "
409+
```"),
410+
#[unstable(feature = "leading_trailing_ones", reason = "newly added", issue = "0")]
411+
#[inline]
412+
pub const fn leading_ones(self) -> u32 {
413+
(self as $UnsignedT).leading_ones()
414+
}
415+
}
416+
417+
doc_comment! {
418+
concat!("Returns the number of trailing ones in the binary representation of `self`.
419+
420+
# Examples
421+
422+
Basic usage:
423+
424+
```
425+
", $Feature, "#![feature(leading_trailing_ones)]
426+
let n = 3", stringify!($SelfT), ";
427+
428+
assert_eq!(n.trailing_ones(), 2);",
429+
$EndFeature, "
430+
```"),
431+
#[unstable(feature = "leading_trailing_ones", reason = "newly added", issue = "0")]
432+
#[inline]
433+
pub const fn trailing_ones(self) -> u32 {
434+
(self as $UnsignedT).trailing_ones()
435+
}
436+
}
437+
396438
doc_comment! {
397439
concat!("Shifts the bits to the left by a specified amount, `n`,
398440
wrapping the truncated bits to the end of the resulting integer.
@@ -2485,6 +2527,47 @@ assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
24852527
}
24862528
}
24872529

2530+
doc_comment! {
2531+
concat!("Returns the number of leading ones in the binary representation of `self`.
2532+
2533+
# Examples
2534+
2535+
Basic usage:
2536+
2537+
```
2538+
", $Feature, "#![feature(leading_trailing_ones)]
2539+
let n = !(", stringify!($SelfT), "::max_value() >> 2);
2540+
2541+
assert_eq!(n.leading_ones(), 2);", $EndFeature, "
2542+
```"),
2543+
#[unstable(feature = "leading_trailing_ones", reason = "newly added", issue = "0")]
2544+
#[inline]
2545+
pub const fn leading_ones(self) -> u32 {
2546+
(!self).leading_zeros()
2547+
}
2548+
}
2549+
2550+
doc_comment! {
2551+
concat!("Returns the number of trailing ones in the binary representation
2552+
of `self`.
2553+
2554+
# Examples
2555+
2556+
Basic usage:
2557+
2558+
```
2559+
", $Feature, "#![feature(leading_trailing_ones)]
2560+
let n = 0b1010111", stringify!($SelfT), ";
2561+
2562+
assert_eq!(n.trailing_ones(), 3);", $EndFeature, "
2563+
```"),
2564+
#[unstable(feature = "leading_trailing_ones", reason = "newly added", issue = "0")]
2565+
#[inline]
2566+
pub const fn trailing_ones(self) -> u32 {
2567+
(!self).trailing_zeros()
2568+
}
2569+
}
2570+
24882571
doc_comment! {
24892572
concat!("Shifts the bits to the left by a specified amount, `n`,
24902573
wrapping the truncated bits to the end of the resulting integer.

0 commit comments

Comments
 (0)