@@ -3281,6 +3281,10 @@ impl BigInt {
3281
3281
3282
3282
/// Sets or clears the bit in the given position,
3283
3283
/// 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
3284
3288
pub fn set_bit ( & mut self , bit : u64 , value : bool ) {
3285
3289
match self . sign {
3286
3290
Sign :: Plus => self . data . set_bit ( bit, value) ,
@@ -3312,7 +3316,8 @@ impl BigInt {
3312
3316
// Clearing the bit at position `trailing_zeros` is dealt with by doing
3313
3317
// similarly to what `bitand_neg_pos` does, except we start at digit
3314
3318
// `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.
3316
3321
let bit_index = ( bit / bits_per_digit) . to_usize ( ) . unwrap ( ) ;
3317
3322
let bit_mask = ( 1 as BigDigit ) << ( bit % bits_per_digit) ;
3318
3323
let mut digit_iter = self . digits_mut ( ) . iter_mut ( ) . skip ( bit_index) ;
@@ -3344,7 +3349,7 @@ impl BigInt {
3344
3349
// |-- bit at position 'bit'
3345
3350
// |-- bit at position 'trailing_zeros'
3346
3351
// 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
3348
3353
let index_lo = ( bit / bits_per_digit) . to_usize ( ) . unwrap ( ) ;
3349
3354
let index_hi = ( trailing_zeros / bits_per_digit) . to_usize ( ) . unwrap ( ) ;
3350
3355
let bit_mask_lo = big_digit:: MAX << ( bit % bits_per_digit) ;
@@ -3355,16 +3360,16 @@ impl BigInt {
3355
3360
if index_lo == index_hi {
3356
3361
digits[ index_lo] ^= bit_mask_lo & bit_mask_hi;
3357
3362
} else {
3358
- digits[ index_lo] ^ = bit_mask_lo;
3363
+ digits[ index_lo] = bit_mask_lo;
3359
3364
for index in ( index_lo + 1 ) ..index_hi {
3360
3365
digits[ index] = big_digit:: MAX ;
3361
3366
}
3362
3367
digits[ index_hi] ^= bit_mask_hi;
3363
3368
}
3364
3369
} else {
3365
3370
// 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
3368
3373
}
3369
3374
}
3370
3375
}
0 commit comments