@@ -78,6 +78,22 @@ pub trait PrimInt:
78
78
/// ```
79
79
fn count_zeros ( self ) -> u32 ;
80
80
81
+ /// Returns the number of leading ones in the binary representation
82
+ /// of `self`.
83
+ ///
84
+ /// # Examples
85
+ ///
86
+ /// ```
87
+ /// use num_traits::PrimInt;
88
+ ///
89
+ /// let n = 0xF00Du16;
90
+ ///
91
+ /// assert_eq!(n.leading_ones(), 4);
92
+ /// ```
93
+ fn leading_ones ( self ) -> u32 {
94
+ ( !self ) . leading_zeros ( )
95
+ }
96
+
81
97
/// Returns the number of leading zeros in the binary representation
82
98
/// of `self`.
83
99
///
@@ -92,6 +108,22 @@ pub trait PrimInt:
92
108
/// ```
93
109
fn leading_zeros ( self ) -> u32 ;
94
110
111
+ /// Returns the number of trailing ones in the binary representation
112
+ /// of `self`.
113
+ ///
114
+ /// # Examples
115
+ ///
116
+ /// ```
117
+ /// use num_traits::PrimInt;
118
+ ///
119
+ /// let n = 0xBEEFu16;
120
+ ///
121
+ /// assert_eq!(n.trailing_ones(), 4);
122
+ /// ```
123
+ fn trailing_ones ( self ) -> u32 {
124
+ ( !self ) . trailing_zeros ( )
125
+ }
126
+
95
127
/// Returns the number of trailing zeros in the binary representation
96
128
/// of `self`.
97
129
///
@@ -319,11 +351,23 @@ macro_rules! prim_int_impl {
319
351
<$T>:: count_zeros( self )
320
352
}
321
353
354
+ #[ cfg( has_leading_leading_ones) ]
355
+ #[ inline]
356
+ fn leading_ones( self ) -> u32 {
357
+ <$T>:: leading_ones( self )
358
+ }
359
+
322
360
#[ inline]
323
361
fn leading_zeros( self ) -> u32 {
324
362
<$T>:: leading_zeros( self )
325
363
}
326
364
365
+ #[ cfg( has_leading_trailing_ones) ]
366
+ #[ inline]
367
+ fn trailing_ones( self ) -> u32 {
368
+ <$T>:: trailing_ones( self )
369
+ }
370
+
327
371
#[ inline]
328
372
fn trailing_zeros( self ) -> u32 {
329
373
<$T>:: trailing_zeros( self )
0 commit comments