Sub subtracts the two bit-arrays bitwise, without carrying the bits. Note that carryless subtraction of a - b is (a and not b). The output is the same as bA, regardless of o's size. If bA is longer than o, o is right padded with zeroes
(o *BitArray)
| 185 | // The output is the same as bA, regardless of o's size. |
| 186 | // If bA is longer than o, o is right padded with zeroes |
| 187 | func (bA *BitArray) Sub(o *BitArray) *BitArray { |
| 188 | if bA == nil || o == nil { |
| 189 | // TODO: Decide if we should do 1's complement here? |
| 190 | return nil |
| 191 | } |
| 192 | bA.mtx.Lock() |
| 193 | o.mtx.Lock() |
| 194 | // output is the same size as bA |
| 195 | c := bA.copyBits(bA.Bits) |
| 196 | // Only iterate to the minimum size between the two. |
| 197 | // If o is longer, those bits are ignored. |
| 198 | // If bA is longer, then skipping those iterations is equivalent |
| 199 | // to right padding with 0's |
| 200 | smaller := tmmath.MinInt(len(bA.Elems), len(o.Elems)) |
| 201 | for i := 0; i < smaller; i++ { |
| 202 | // &^ is and not in golang |
| 203 | c.Elems[i] &^= o.Elems[i] |
| 204 | } |
| 205 | bA.mtx.Unlock() |
| 206 | o.mtx.Unlock() |
| 207 | return c |
| 208 | } |
| 209 | |
| 210 | // IsEmpty returns true iff all bits in the bit array are 0 |
| 211 | func (bA *BitArray) IsEmpty() bool { |