File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,28 @@ macro_rules! int_impl {
167167 ( self as $UnsignedT) . trailing_ones( )
168168 }
169169
170+ /// Returns whether the `k`-th least-significant bit in the binary
171+ /// representation of `self` is a one.
172+ ///
173+ /// Returns `false` if the supplied index is greater than or equal to the
174+ /// number of bits of the binary representation of `self`.
175+ ///
176+ /// # Examples
177+ ///
178+ /// ```
179+ #[ doc = concat!( "let n = 0b00000101" , stringify!( $SelfT) , ";" ) ]
180+ /// assert_eq!(n.is_bit_one(0), true);
181+ /// assert_eq!(n.is_bit_one(1), false);
182+ /// assert_eq!(n.is_bit_one(2), true);
183+ /// assert_eq!(n.is_bit_one(99999), false);
184+ /// ```
185+ #[ stable( feature = "leading_trailing_ones" , since = "1.46.0" ) ]
186+ #[ rustc_const_stable( feature = "leading_trailing_ones" , since = "1.46.0" ) ]
187+ #[ inline( always) ]
188+ pub const fn is_bit_one( self , k: usize ) -> bool {
189+ ( ( 1 << k) & self ) > 0
190+ }
191+
170192 /// Returns `self` with only the most significant bit set, or `0` if
171193 /// the input is `0`.
172194 ///
Original file line number Diff line number Diff line change @@ -200,6 +200,28 @@ macro_rules! uint_impl {
200200 ( !self ) . trailing_zeros( )
201201 }
202202
203+ /// Returns whether the `k`-th least-significant bit in the binary
204+ /// representation of `self` is a one.
205+ ///
206+ /// Returns `false` if the supplied index is greater than or equal to the
207+ /// number of bits of the binary representation of `self`.
208+ ///
209+ /// # Examples
210+ ///
211+ /// ```
212+ #[ doc = concat!( "let n = 0b00000101" , stringify!( $SelfT) , ";" ) ]
213+ /// assert_eq!(n.is_bit_one(0), true);
214+ /// assert_eq!(n.is_bit_one(1), false);
215+ /// assert_eq!(n.is_bit_one(2), true);
216+ /// assert_eq!(n.is_bit_one(99999), false);
217+ /// ```
218+ #[ stable( feature = "leading_trailing_ones" , since = "1.46.0" ) ]
219+ #[ rustc_const_stable( feature = "leading_trailing_ones" , since = "1.46.0" ) ]
220+ #[ inline( always) ]
221+ pub const fn is_bit_one( self , k: usize ) -> bool {
222+ ( ( 1 << k) & self ) > 0
223+ }
224+
203225 /// Returns the minimum number of bits required to represent `self`.
204226 ///
205227 /// This method returns zero if `self` is zero.
You can’t perform that action at this time.
0 commit comments