Last active
January 8, 2021 17:06
-
-
Save icholy/3b108ce6e0b615b56bb19d0226dbef83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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