@@ -3285,10 +3285,17 @@ impl BigInt {
3285
3285
}
3286
3286
Sign :: Minus => {
3287
3287
let bits_per_digit = u64:: from ( big_digit:: BITS ) ;
3288
- if bit < bits_per_digit * self . len ( ) as u64 {
3289
- // This implementation corresponds to what the function `bitand_neg_pos` does when
3290
- // value=false and what `bitor_neg_pos` does when value=true, except there is no
3291
- // need to explicitly iterate over the digits of the right-hand side
3288
+ // The first part of this `if` condition is not necessary but included because
3289
+ // computing trailing_zeros can be avoided when the bit to set/clear is outside
3290
+ // the represented digits
3291
+ if bit >= bits_per_digit * self . len ( ) as u64
3292
+ || bit > self . data . trailing_zeros ( ) . unwrap ( )
3293
+ {
3294
+ self . data . set_bit ( bit, !value) ;
3295
+ } else {
3296
+ // This is the general case that basically corresponds to what `bitor_neg_pos`
3297
+ // (when setting bit) or `bitand_neg_pos` (when clearing bit) does, except there
3298
+ // is no need to explicitly iterate over the digits of the right-hand side
3292
3299
let bit_index = ( bit / bits_per_digit) as usize ;
3293
3300
let bit_mask = ( 1 as BigDigit ) << ( bit % bits_per_digit) ;
3294
3301
let mut carry_in = 1 ;
@@ -3304,13 +3311,6 @@ impl BigInt {
3304
3311
} ;
3305
3312
* digit = negate_carry ( twos_out, & mut carry_out) ;
3306
3313
}
3307
- } else {
3308
- // The bit to set/clear is outside the represented digits, and thus more significant
3309
- // than the most significant bit of the current number. This corresponds to setting
3310
- // the bit to the negated value (no-op for value=true)
3311
- if !value {
3312
- self . data . set_bit ( bit, true ) ;
3313
- }
3314
3314
}
3315
3315
}
3316
3316
}
0 commit comments