Skip to content

Commit d95583c

Browse files
committed
Some more comments
1 parent df4ef9d commit d95583c

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/bigint.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,10 @@ impl BigInt {
32813281

32823282
/// Sets or clears the bit in the given position,
32833283
/// using the two's complement for negative numbers
3284+
///
3285+
/// Note that setting/clearing a bit (for positive/negative numbers,
3286+
/// respectively) greater than the current bit length, a reallocation
3287+
/// may be needed to store the new digits
32843288
pub fn set_bit(&mut self, bit: u64, value: bool) {
32853289
match self.sign {
32863290
Sign::Plus => self.data.set_bit(bit, value),
@@ -3312,7 +3316,8 @@ impl BigInt {
33123316
// Clearing the bit at position `trailing_zeros` is dealt with by doing
33133317
// similarly to what `bitand_neg_pos` does, except we start at digit
33143318
// `bit_index`. All digits below `bit_index` are guaranteed to be zero,
3315-
// so initially we have `carry_in` = `carry_out` = 1.
3319+
// so initially we have `carry_in` = `carry_out` = 1. Furthermore, we
3320+
// stop traversing the digits when there are no more carries.
33163321
let bit_index = (bit / bits_per_digit).to_usize().unwrap();
33173322
let bit_mask = (1 as BigDigit) << (bit % bits_per_digit);
33183323
let mut digit_iter = self.digits_mut().iter_mut().skip(bit_index);
@@ -3344,7 +3349,7 @@ impl BigInt {
33443349
// |-- bit at position 'bit'
33453350
// |-- bit at position 'trailing_zeros'
33463351
// bit_mask: 1 1 ... 1 0 .. 0
3347-
// We do this by xor'ing with the bit_mask
3352+
// This is done by xor'ing with the bit_mask
33483353
let index_lo = (bit / bits_per_digit).to_usize().unwrap();
33493354
let index_hi = (trailing_zeros / bits_per_digit).to_usize().unwrap();
33503355
let bit_mask_lo = big_digit::MAX << (bit % bits_per_digit);
@@ -3355,16 +3360,16 @@ impl BigInt {
33553360
if index_lo == index_hi {
33563361
digits[index_lo] ^= bit_mask_lo & bit_mask_hi;
33573362
} else {
3358-
digits[index_lo] ^= bit_mask_lo;
3363+
digits[index_lo] = bit_mask_lo;
33593364
for index in (index_lo + 1)..index_hi {
33603365
digits[index] = big_digit::MAX;
33613366
}
33623367
digits[index_hi] ^= bit_mask_hi;
33633368
}
33643369
} else {
33653370
// We end up here in two cases:
3366-
// * bit == trailing_zeros && value: Bit is already set
3367-
// * bit < trailing_zeros && !value: Bit is already cleared
3371+
// bit == trailing_zeros && value: Bit is already set
3372+
// bit < trailing_zeros && !value: Bit is already cleared
33683373
}
33693374
}
33703375
}

0 commit comments

Comments
 (0)