Skip to content

Commit 5aaf369

Browse files
committed
Let BigUint::bit work to the extreme u64::MAX
32-bit targets would fail to convert that to a `usize` digit index, but we can reasonably say that all bits are false at that point.
1 parent d95583c commit 5aaf369

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/biguint.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,9 +2729,13 @@ impl BigUint {
27292729
/// Returns whether the bit in the given position is set
27302730
pub fn bit(&self, bit: u64) -> bool {
27312731
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
27352739
}
27362740

27372741
/// Sets or clears the bit in the given position

tests/bigint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,13 +1317,15 @@ fn test_bit() {
13171317
assert!(BigInt::from(0b1100u8).bit(3));
13181318
assert!(!BigInt::from(0b1100u8).bit(4));
13191319
assert!(!BigInt::from(0b1100u8).bit(200));
1320+
assert!(!BigInt::from(0b1100u8).bit(u64::MAX));
13201321
// -12 = (...110100)_2
13211322
assert!(!BigInt::from(-12i8).bit(0));
13221323
assert!(!BigInt::from(-12i8).bit(1));
13231324
assert!(BigInt::from(-12i8).bit(2));
13241325
assert!(!BigInt::from(-12i8).bit(3));
13251326
assert!(BigInt::from(-12i8).bit(4));
13261327
assert!(BigInt::from(-12i8).bit(200));
1328+
assert!(BigInt::from(-12i8).bit(u64::MAX));
13271329
}
13281330

13291331
#[test]

0 commit comments

Comments
 (0)