File tree Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Expand file tree Collapse file tree 2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -2729,9 +2729,13 @@ impl BigUint {
2729
2729
/// Returns whether the bit in the given position is set
2730
2730
pub fn bit ( & self , bit : u64 ) -> bool {
2731
2731
let bits_per_digit = u64:: from ( big_digit:: BITS ) ;
2732
- let digit_index = ( bit / bits_per_digit) . to_usize ( ) . unwrap ( ) ;
2733
- digit_index < self . data . len ( )
2734
- && ( self . data [ digit_index] & ( ( 1 as BigDigit ) << ( bit % bits_per_digit) ) ) != 0
2732
+ if let Some ( digit_index) = ( bit / bits_per_digit) . to_usize ( ) {
2733
+ if let Some ( digit) = self . data . get ( digit_index) {
2734
+ let bit_mask = ( 1 as BigDigit ) << ( bit % bits_per_digit) ;
2735
+ return ( digit & bit_mask) != 0 ;
2736
+ }
2737
+ }
2738
+ false
2735
2739
}
2736
2740
2737
2741
/// Sets or clears the bit in the given position
Original file line number Diff line number Diff line change @@ -1317,13 +1317,15 @@ fn test_bit() {
1317
1317
assert ! ( BigInt :: from( 0b1100u8 ) . bit( 3 ) ) ;
1318
1318
assert ! ( !BigInt :: from( 0b1100u8 ) . bit( 4 ) ) ;
1319
1319
assert ! ( !BigInt :: from( 0b1100u8 ) . bit( 200 ) ) ;
1320
+ assert ! ( !BigInt :: from( 0b1100u8 ) . bit( u64 :: MAX ) ) ;
1320
1321
// -12 = (...110100)_2
1321
1322
assert ! ( !BigInt :: from( -12i8 ) . bit( 0 ) ) ;
1322
1323
assert ! ( !BigInt :: from( -12i8 ) . bit( 1 ) ) ;
1323
1324
assert ! ( BigInt :: from( -12i8 ) . bit( 2 ) ) ;
1324
1325
assert ! ( !BigInt :: from( -12i8 ) . bit( 3 ) ) ;
1325
1326
assert ! ( BigInt :: from( -12i8 ) . bit( 4 ) ) ;
1326
1327
assert ! ( BigInt :: from( -12i8 ) . bit( 200 ) ) ;
1328
+ assert ! ( BigInt :: from( -12i8 ) . bit( u64 :: MAX ) ) ;
1327
1329
}
1328
1330
1329
1331
#[ test]
You can’t perform that action at this time.
0 commit comments