GetBitAtIndex extract bit at given index in the BitArray.
(index int)
| 580 | |
| 581 | // GetBitAtIndex extract bit at given index in the BitArray. |
| 582 | func (d BitArray) GetBitAtIndex(index int) (int, error) { |
| 583 | // Check whether index asked is inside BitArray. |
| 584 | if index < 0 || uint(index) >= d.BitLen() { |
| 585 | return 0, pgerror.Newf(pgcode.ArraySubscript, "bit index %d out of valid range (0..%d)", index, int(d.BitLen())-1) |
| 586 | } |
| 587 | // To extract bit at the given index, we have to determine the |
| 588 | // position within words array, i.e. index/numBitsPerWord after |
| 589 | // that checked the bit at residual index. |
| 590 | if d.words[index/numBitsPerWord]&(word(1)<<(numBitsPerWord-1-uint(index)%numBitsPerWord)) != 0 { |
| 591 | return 1, nil |
| 592 | } |
| 593 | return 0, nil |
| 594 | } |
| 595 | |
| 596 | // SetBitAtIndex returns the BitArray with an updated bit at a given index. |
| 597 | func (d BitArray) SetBitAtIndex(index, toSet int) (BitArray, error) { |