SetBitAtIndex returns the BitArray with an updated bit at a given index.
(index, toSet int)
| 572 | |
| 573 | // SetBitAtIndex returns the BitArray with an updated bit at a given index. |
| 574 | func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error) { |
| 575 | res := d.Clone() |
| 576 | // Check whether index asked is inside BitArray. |
| 577 | if index < 0 || uint(index) >= res.BitLen() { |
| 578 | err := fmt.Errorf("SetBitAtIndex: bit index %d out of valid range (0..%d)", index, int(res.BitLen())-1) |
| 579 | return BitArray{}, pgerror.WithCandidateCode(err, pgcode.ArraySubscript) |
| 580 | } |
| 581 | // To update bit at the given index, we have to determine the |
| 582 | // position within words array, i.e. index/numBitsPerWord after |
| 583 | // that updated the bit at residual index. |
| 584 | // Forcefully making bit at the index to 0. |
| 585 | res.words[index/numBitsPerWord] &= ^(word(1) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord)) |
| 586 | // Updating value at the index to toSet. |
| 587 | res.words[index/numBitsPerWord] |= word(toSet) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord) |
| 588 | return res, nil |
| 589 | } |
nothing calls this directly
no test coverage detected