Skip to content

Commit 8d6d388

Browse files
committed
Some conversion and overflow checks
1 parent 40ce776 commit 8d6d388

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/bigint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ impl BigInt {
32563256
}
32573257

32583258
/// Returns whether the bit in position `bit` is set,
3259-
/// uses the two's complement for negative numbers
3259+
/// using the two's complement for negative numbers
32603260
pub fn bit(&self, bit: u64) -> bool {
32613261
// Let the binary representation of a number be
32623262
// ... 0 x 1 0 ... 0
@@ -3273,7 +3273,7 @@ impl BigInt {
32733273
}
32743274

32753275
/// Sets or clears the bit in the given position,
3276-
/// uses the two's complement for negative numbers
3276+
/// using the two's complement for negative numbers
32773277
pub fn set_bit(&mut self, bit: u64, value: bool) {
32783278
match self.sign {
32793279
Sign::Plus => self.data.set_bit(bit, value),
@@ -3297,7 +3297,7 @@ impl BigInt {
32973297
// This is the general case that basically corresponds to what `bitor_neg_pos`
32983298
// (when setting bit) or `bitand_neg_pos` (when clearing bit) does, except there
32993299
// is no need to explicitly iterate over the digits of the right-hand side
3300-
let bit_index = (bit / bits_per_digit) as usize;
3300+
let bit_index = (bit / bits_per_digit).to_usize().unwrap();
33013301
let bit_mask = (1 as BigDigit) << (bit % bits_per_digit);
33023302
let mut carry_in = 1;
33033303
let mut carry_out = 1;

src/biguint.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,19 +2729,23 @@ 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) as usize;
2732+
let digit_index = (bit / bits_per_digit).to_usize().unwrap();
27332733
digit_index < self.data.len()
27342734
&& (self.data[digit_index] & ((1 as BigDigit) << (bit % bits_per_digit))) != 0
27352735
}
27362736

27372737
/// Sets or clears the bit in the given position
2738+
///
2739+
/// Note that setting a bit greater than the current bit length, a reallocation may be needed
2740+
/// to store the new digits
27382741
pub fn set_bit(&mut self, bit: u64, value: bool) {
27392742
let bits_per_digit = u64::from(big_digit::BITS);
2740-
let digit_index = (bit / bits_per_digit) as usize;
2743+
let digit_index = (bit / bits_per_digit).to_usize().unwrap();
27412744
let bit_mask = (1 as BigDigit) << (bit % bits_per_digit);
27422745
if value {
27432746
if digit_index >= self.data.len() {
2744-
self.data.resize(digit_index + 1, 0);
2747+
let new_len = digit_index.checked_add(1).unwrap();
2748+
self.data.resize(new_len, 0);
27452749
}
27462750
self.data[digit_index] |= bit_mask;
27472751
} else if digit_index < self.data.len() {

0 commit comments

Comments
 (0)