英文:
How to shift byte array with Golang?
问题
以下是将字节数组左移的简单工作代码:
package main
import (
"fmt"
)
type Byte byte
func SL(b Byte) Byte {
if b&0x80 == 0x80 {
b <<= 1
b ^= 0x01
} else {
b <<= 1
}
return b
}
func main() {
var b Byte
b = 0xD3
fmt.Printf("old byte %#08b\n", b) // 11010011
c := SL(b)
fmt.Printf("new byte %#08b", c) // 10100111
}
要将字节数组左移,你可以使用以下代码:
package main
import (
"fmt"
)
type Byte [2]byte
func SL(b Byte) Byte {
for i := 0; i < len(b); i++ {
if b[i]&0x80 == 0x80 {
b[i] <<= 1
b[i] ^= 0x01
} else {
b[i] <<= 1
}
}
return b
}
func main() {
var b Byte
b = [2]byte{0xD3, 0x5A}
fmt.Printf("old bytes %#016b\n", b) // 11010011 01011010
c := SL(b)
fmt.Printf("new bytes %#016b", c) // 10100111 10110101
}
希望对你有帮助!
英文:
Here simple working code to left shift first bit of a byte
package main
import (
"fmt"
)
type Byte byte
func SL(b Byte) Byte {
if b&0x80 == 0x80 {
b <<= 1
b ^= 0x01
} else {
b <<= 1
}
return b
}
func main() {
var b Byte
b = 0xD3
fmt.Printf("old byte %#08b\n", b) // 11010011
c := SL(b)
fmt.Printf("new byte %#08b", c) // 10100111
}
What should I do to shift array of bytes, like
type Byte [2]byte
?
Thanks for advance!
答案1
得分: 3
你似乎想要进行旋转,而不是移位。有什么特殊原因你不使用uint16
类型而是[2]byte
类型?
无论如何,如果你真的想要[2]byte
类型,这个方法更简单,而且没有分支:
func rol(v [2]byte) [2]byte {
x := int(v[0])<<8 | int(v[1])
x <<= 1
v[0] = byte(x >> 8)
v[1] = byte((x & 0xff) | x>>16)
return v
}
如果你想在任意大的位数上执行这样的操作,你可以使用math/big
。
英文:
You appear to want to rotate, not shift. Any particular reason you aren't using a uint16
type instead of [2]byte
?
Anyway, if you really want [2]byte
, this is simpler and doesn't branch:
func rol(v [2]byte) [2]byte {
x := int(v[0])<<8 | int(v[1])
x <<= 1
v[0] = byte(x >> 8)
v[1] = byte((x & 0xff) | x>>16)
return v
}
If you want to do such operations on an arbitrary large number of bits you could use math/big
.
答案2
得分: 3
一个左移1位的解决方案。
func shiftBytesLeft(a []byte) (dst []byte) {
n := len(a)
dst = make([]byte, n)
for i := 0; i < n-1; i++ {
dst[i] = a[i] << 1
dst[i] = (dst[i] & 0xfe) | (a[i+1] >> 7)
}
dst[n-1] = a[n-1] << 1
return dst
}
一个左移1位的解决方案。
英文:
A solution to shift left 1 bit.
func shiftBytesLeft(a []byte) (dst []byte) {
n := len(a)
dst = make([]byte, n)
for i := 0; i < n-1; i++ {
dst[i] = a[i] << 1
dst[i] = (dst[i] & 0xfe) | (a[i+1] >> 7)
}
dst[n-1] = a[n-1] << 1
return dst
}
答案3
得分: 2
这是一个可以进行左移和右移操作的实现:
// ShiftLeft对提供的字节进行左位移操作。
// 如果位数是负数,则执行右位移操作。
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]>>bits | data[i-1]<<(8-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
}
}
英文:
Here's an implementation that can do both left and right shifts:
// 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]>>bits | data[i-1]<<(8-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
}
}
答案4
得分: 1
是的!我找到了一个解决方案。
package main
import (
"fmt"
)
type Byte [2]byte
//左移
func SL(b Byte) Byte {
if b[0]&0x80 == 0x80 {
b[0] <<= 1
if b[1]&0x80 == 0x80 {
b[0] ^= 1
b[1] <<= 1
} else {
b[1] <<= 1
}
b[1] ^= 0x01
} else {
b[0] <<= 1
if b[1]&0x80 == 0x80 {
b[0] ^= 1
b[1] <<= 1
} else {
b[1] <<= 1
}
}
return b
}
func main() {
b := Byte{0x23, 0x86}
fmt.Printf("旧字节 %#08b %#08b\n", b[0], b[1]) // 00100011 10000110
c := SL(b)
fmt.Printf("新字节 %#08b %#08b", c[0], c[1]) // 01000111 00001100
}
英文:
Yep! I found a solution.
package main
import (
"fmt"
)
type Byte [2]byte
//shift left
func SL(b Byte) Byte {
if b[0]&0x80 == 0x80 {
b[0] <<= 1
if b[1]&0x80 == 0x80 {
b[0] ^= 1
b[1] <<= 1
} else {
b[1] <<= 1
}
b[1] ^= 0x01
} else {
b[0] <<= 1
if b[1]&0x80 == 0x80 {
b[0] ^= 1
b[1] <<= 1
} else {
b[1] <<= 1
}
}
return b
}
func main() {
//var b Byte
b := Byte{0x23, 0x86}
fmt.Printf("old byte %#08b %#08b\n", b[0], b[1]) // 00100011 10000110
c := SL(b)
fmt.Printf("new byte %#08b %#08b", c[0], c[1]) // 01000111 00001100
}
答案5
得分: 1
将qin的答案进行位左移,扩展如下:
func shiftBytesLeft(a []byte, byBits int) (dst []byte) {
n := len(a)
dst = make([]byte, n)
for i := 0; i < n-1; i++ {
dst[i] = a[i] << byBits
dst[i] = (dst[i] & (0xff - byte(byBits))) | (a[i+1] >> (8 - byte(byBits)))
}
dst[n-1] = a[n-1] << byBits
return dst
}
英文:
shift left by multiple bits, expanding qin's answer:
func shiftBytesLeft(a []byte, byBits int) (dst []byte) {
n := len(a)
dst = make([]byte, n)
for i := 0; i < n-1; i++ {
dst[i] = a[i] << byBits
dst[i] = (dst[i] & (0xff - byte(byBits))) | (a[i+1] >> (8 - byte(byBits)))
}
dst[n-1] = a[n-1] << byBits
return dst
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论