MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / SetBitAtIndex

Method SetBitAtIndex

pkg/util/bitarray/bitarray.go:597–611  ·  view source on GitHub ↗

SetBitAtIndex returns the BitArray with an updated bit at a given index.

(index, toSet int)

Source from the content-addressed store, hash-verified

595
596// SetBitAtIndex returns the BitArray with an updated bit at a given index.
597func (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.

Callers

nothing calls this directly

Calls 3

CloneMethod · 0.95
NewfFunction · 0.92
BitLenMethod · 0.80

Tested by

no test coverage detected