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

Method LeftShiftAny

pkg/util/bitarray/bitarray.go:206–254  ·  view source on GitHub ↗

LeftShiftAny performs a logical left shift, with a possible negative count. The number of bits to shift can be arbitrarily large (i.e. possibly larger than 64 in absolute value).

(n int64)

Source from the content-addressed store, hash-verified

204// The number of bits to shift can be arbitrarily large (i.e. possibly
205// larger than 64 in absolute value).
206func (d BitArray) LeftShiftAny(n int64) BitArray {
207 bitlen := d.BitLen()
208 if n == 0 || bitlen == 0 {
209 // Fast path.
210 return d
211 }
212
213 r := MakeZeroBitArray(bitlen)
214 if (n > 0 && n > int64(bitlen)) || (n < 0 && -n > int64(bitlen)) {
215 // Fast path.
216 return r
217 }
218
219 if n > 0 {
220 // This is a left shift.
221 dstWord := uint(0)
222 srcWord := uint(uint64(n) / numBitsPerWord)
223 srcShift := uint(uint64(n) % numBitsPerWord)
224 for i, j := srcWord, dstWord; i < uint(len(d.words)); i++ {
225 r.words[j] = d.words[i] << srcShift
226 j++
227 }
228 for i, j := srcWord+1, dstWord; i < uint(len(d.words)); i++ {
229 r.words[j] |= d.words[i] >> (numBitsPerWord - srcShift)
230 j++
231 }
232 } else {
233 // A right shift.
234 n = -n
235 srcWord := uint(0)
236 dstWord := uint(uint64(n) / numBitsPerWord)
237 srcShift := uint(uint64(n) % numBitsPerWord)
238 for i, j := srcWord, dstWord; j < uint(len(r.words)); i++ {
239 r.words[j] = d.words[i] >> srcShift
240 j++
241 }
242 for i, j := srcWord, dstWord+1; j < uint(len(r.words)); i++ {
243 r.words[j] |= d.words[i] << (numBitsPerWord - srcShift)
244 j++
245 }
246 // Erase the trailing bits that are not used any more.
247 // See #36606.
248 if len(r.words) > 0 {
249 r.words[len(r.words)-1] &= ^word(0) << (numBitsPerWord - r.lastBitsUsed)
250 }
251 }
252
253 return r
254}
255
256// byteReprs contains the bit representation of the 256 possible
257// groups of 8 bits.

Callers

nothing calls this directly

Calls 2

BitLenMethod · 0.95
MakeZeroBitArrayFunction · 0.85

Tested by

no test coverage detected