Skip to content

Instantly share code, notes, and snippets.

@icholy
Last active January 8, 2021 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/3b108ce6e0b615b56bb19d0226dbef83 to your computer and use it in GitHub Desktop.
Save icholy/3b108ce6e0b615b56bb19d0226dbef83 to your computer and use it in GitHub Desktop.
package binutil
// ShiftLeft performs a left bit shift operation on the provided bytes.
// If the bits count is negative, a right bit shift is performed.
func ShiftLeft(data []byte, bits int) {
n := len(data)
if bits < 0 {
bits = -bits
for i := n - 1; i > 0; i-- {
data[i] = data[i-1]<<(8-bits) | data[i]>>bits
}
data[0] >>= bits
} else {
for i := 0; i < n-1; i++ {
data[i] = data[i]<<bits | data[i+1]>>(8-bits)
}
data[n-1] <<= bits
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment