@@ -100,8 +100,20 @@ func (b *BSI) SetBigValue(columnID uint64, value *big.Int) {
100100 if minBits == 1 {
101101 minBits = 2
102102 }
103- for len (b .bA ) < minBits {
104- b .bA = append (b .bA , Bitmap {})
103+ if len (b .bA ) < minBits {
104+ oldSignPos := len (b .bA ) - 1
105+ for len (b .bA ) < minBits {
106+ b .bA = append (b .bA , Bitmap {})
107+ }
108+ // When bA grows, the sign slot shifts from oldSignPos to the new end
109+ // of bA. For existing negative entries (whose sign bit is set in
110+ // bA[oldSignPos]), sign-extension requires that all intermediate bit
111+ // positions between oldSignPos and the new sign position also be set.
112+ // Copy the old sign bitmap into every new slot (sign extension).
113+ newSignPos := len (b .bA ) - 1
114+ for i := oldSignPos + 1 ; i <= newSignPos ; i ++ {
115+ b .bA [i ].Or (& b .bA [oldSignPos ])
116+ }
105117 }
106118 }
107119
@@ -122,8 +134,16 @@ func (b *BSI) SetBigMany(foundSet *Bitmap, value *big.Int) {
122134 if minBits == 1 {
123135 minBits = 2
124136 }
125- for len (b .bA ) < minBits {
126- b .bA = append (b .bA , Bitmap {})
137+ if len (b .bA ) < minBits {
138+ oldSignPos := len (b .bA ) - 1
139+ for len (b .bA ) < minBits {
140+ b .bA = append (b .bA , Bitmap {})
141+ }
142+ // Sign-extend existing negative entries into the new bit slots.
143+ newSignPos := len (b .bA ) - 1
144+ for i := oldSignPos + 1 ; i <= newSignPos ; i ++ {
145+ b .bA [i ].Or (& b .bA [oldSignPos ])
146+ }
127147 }
128148 }
129149 for i := b .BitCount (); i >= 0 ; i -- {
0 commit comments