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