Skip to content

Commit 0f70e1a

Browse files
committed
bitv: reuse mask_for_bits() in grow()
1 parent 2a6e5b1 commit 0f70e1a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/libcollections/bit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,15 +794,15 @@ impl Bitv {
794794
let new_nblocks = blocks_for_bits(new_nbits);
795795
let full_value = if value { !0 } else { 0 };
796796

797-
// Correct the old tail word
797+
// Correct the old tail word, setting or clearing formerly unused bits
798798
let old_last_word = blocks_for_bits(self.nbits) - 1;
799799
if self.nbits % u32::BITS > 0 {
800-
let overhang = self.nbits % u32::BITS; // # of already-used bits
801-
let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110..0
800+
let mask = mask_for_bits(self.nbits);
802801
if value {
803-
self.storage[old_last_word] |= mask;
802+
self.storage[old_last_word] |= !mask;
804803
} else {
805-
self.storage[old_last_word] &= !mask;
804+
// Extra bits are already supposed to be zero by invariant, but play it safe...
805+
self.storage[old_last_word] &= mask;
806806
}
807807
}
808808

0 commit comments

Comments
 (0)