SetBitAtIndex returns the BitArray with an updated bit at a given index.
(index, toSet int)
| 558 | |
| 559 | // SetBitAtIndex returns the BitArray with an updated bit at a given index. |
| 560 | func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error) { |
| 561 | res := d.Clone() |
| 562 | // Check whether index asked is inside BitArray. |
| 563 | if index < 0 || uint(index) >= res.BitLen() { |
| 564 | err := fmt.Errorf("SetBitAtIndex: bit index %d out of valid range (0..%d)", index, int(res.BitLen())-1) |
| 565 | return BitArray{}, pgerror.WithCandidateCode(err, pgcode.ArraySubscript) |
| 566 | } |
| 567 | // To update bit at the given index, we have to determine the |
| 568 | // position within words array, i.e. index/numBitsPerWord after |
| 569 | // that updated the bit at residual index. |
| 570 | // Forcefully making bit at the index to 0. |
| 571 | res.words[index/numBitsPerWord] &= ^(word(1) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord)) |
| 572 | // Updating value at the index to toSet. |
| 573 | res.words[index/numBitsPerWord] |= word(toSet) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord) |
| 574 | return res, nil |
| 575 | } |
nothing calls this directly
no test coverage detected