SetBitAtIndex returns the BitArray with an updated bit at a given index.
(index, toSet int)
| 595 | |
| 596 | // SetBitAtIndex returns the BitArray with an updated bit at a given index. |
| 597 | func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error) { |
| 598 | res := d.Clone() |
| 599 | // Check whether index asked is inside BitArray. |
| 600 | if index < 0 || uint(index) >= res.BitLen() { |
| 601 | return BitArray{}, pgerror.Newf(pgcode.ArraySubscript, "bit index %d out of valid range (0..%d)", index, int(res.BitLen())-1) |
| 602 | } |
| 603 | // To update bit at the given index, we have to determine the |
| 604 | // position within words array, i.e. index/numBitsPerWord after |
| 605 | // that updated the bit at residual index. |
| 606 | // Forcefully making bit at the index to 0. |
| 607 | res.words[index/numBitsPerWord] &= ^(word(1) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord)) |
| 608 | // Updating value at the index to toSet. |
| 609 | res.words[index/numBitsPerWord] |= word(toSet) << (numBitsPerWord - 1 - uint(index)%numBitsPerWord) |
| 610 | return res, nil |
| 611 | } |
| 612 | |
| 613 | // AsUInt64 returns the uint64 constituted from the rightmost bits in the |
| 614 | // bit array. |