如何使用Golang移动字节数组?

huangapple go评论117阅读模式
英文:

How to shift byte array with Golang?

问题

以下是将字节数组左移的简单工作代码:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Byte byte
  6. func SL(b Byte) Byte {
  7. if b&0x80 == 0x80 {
  8. b <<= 1
  9. b ^= 0x01
  10. } else {
  11. b <<= 1
  12. }
  13. return b
  14. }
  15. func main() {
  16. var b Byte
  17. b = 0xD3
  18. fmt.Printf("old byte %#08b\n", b) // 11010011
  19. c := SL(b)
  20. fmt.Printf("new byte %#08b", c) // 10100111
  21. }

要将字节数组左移,你可以使用以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Byte [2]byte
  6. func SL(b Byte) Byte {
  7. for i := 0; i < len(b); i++ {
  8. if b[i]&0x80 == 0x80 {
  9. b[i] <<= 1
  10. b[i] ^= 0x01
  11. } else {
  12. b[i] <<= 1
  13. }
  14. }
  15. return b
  16. }
  17. func main() {
  18. var b Byte
  19. b = [2]byte{0xD3, 0x5A}
  20. fmt.Printf("old bytes %#016b\n", b) // 11010011 01011010
  21. c := SL(b)
  22. fmt.Printf("new bytes %#016b", c) // 10100111 10110101
  23. }

希望对你有帮助!

英文:

Here simple working code to left shift first bit of a byte

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. type Byte byte
  6. func SL(b Byte) Byte {
  7. if b&amp;0x80 == 0x80 {
  8. b &lt;&lt;= 1
  9. b ^= 0x01
  10. } else {
  11. b &lt;&lt;= 1
  12. }
  13. return b
  14. }
  15. func main() {
  16. var b Byte
  17. b = 0xD3
  18. fmt.Printf(&quot;old byte %#08b\n&quot;, b) // 11010011
  19. c := SL(b)
  20. fmt.Printf(&quot;new byte %#08b&quot;, c) // 10100111
  21. }

What should I do to shift array of bytes, like
type Byte [2]byte?

Thanks for advance!

答案1

得分: 3

你似乎想要进行旋转,而不是移位。有什么特殊原因你不使用uint16类型而是[2]byte类型?

无论如何,如果你真的想要[2]byte类型,这个方法更简单,而且没有分支:

  1. func rol(v [2]byte) [2]byte {
  2. x := int(v[0])<<8 | int(v[1])
  3. x <<= 1
  4. v[0] = byte(x >> 8)
  5. v[1] = byte((x & 0xff) | x>>16)
  6. return v
  7. }

如果你想在任意大的位数上执行这样的操作,你可以使用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:

  1. func rol(v [2]byte) [2]byte {
  2. x := int(v[0])&lt;&lt;8 | int(v[1])
  3. x &lt;&lt;= 1
  4. v[0] = byte(x &gt;&gt; 8)
  5. v[1] = byte((x &amp; 0xff) | x&gt;&gt;16)
  6. return v
  7. }

If you want to do such operations on an arbitrary large number of bits you could use math/big.

答案2

得分: 3

一个左移1位的解决方案。

  1. func shiftBytesLeft(a []byte) (dst []byte) {
  2. n := len(a)
  3. dst = make([]byte, n)
  4. for i := 0; i < n-1; i++ {
  5. dst[i] = a[i] << 1
  6. dst[i] = (dst[i] & 0xfe) | (a[i+1] >> 7)
  7. }
  8. dst[n-1] = a[n-1] << 1
  9. return dst
  10. }

一个左移1位的解决方案。

英文:

A solution to shift left 1 bit.

  1. func shiftBytesLeft(a []byte) (dst []byte) {
  2. n := len(a)
  3. dst = make([]byte, n)
  4. for i := 0; i &lt; n-1; i++ {
  5. dst[i] = a[i] &lt;&lt; 1
  6. dst[i] = (dst[i] &amp; 0xfe) | (a[i+1] &gt;&gt; 7)
  7. }
  8. dst[n-1] = a[n-1] &lt;&lt; 1
  9. return dst
  10. }

答案3

得分: 2

这是一个可以进行左移和右移操作的实现:

  1. // ShiftLeft对提供的字节进行左位移操作。
  2. // 如果位数是负数,则执行右位移操作。
  3. func ShiftLeft(data []byte, bits int) {
  4. n := len(data)
  5. if bits < 0 {
  6. bits = -bits
  7. for i := n - 1; i > 0; i-- {
  8. data[i] = data[i]>>bits | data[i-1]<<(8-bits)
  9. }
  10. data[0] >>= bits
  11. } else {
  12. for i := 0; i < n-1; i++ {
  13. data[i] = data[i]<<bits | data[i+1]>>(8-bits)
  14. }
  15. data[n-1] <<= bits
  16. }
  17. }
英文:

Here's an implementation that can do both left and right shifts:

  1. // ShiftLeft performs a left bit shift operation on the provided bytes.
  2. // If the bits count is negative, a right bit shift is performed.
  3. func ShiftLeft(data []byte, bits int) {
  4. n := len(data)
  5. if bits &lt; 0 {
  6. bits = -bits
  7. for i := n - 1; i &gt; 0; i-- {
  8. data[i] = data[i]&gt;&gt;bits | data[i-1]&lt;&lt;(8-bits)
  9. }
  10. data[0] &gt;&gt;= bits
  11. } else {
  12. for i := 0; i &lt; n-1; i++ {
  13. data[i] = data[i]&lt;&lt;bits | data[i+1]&gt;&gt;(8-bits)
  14. }
  15. data[n-1] &lt;&lt;= bits
  16. }
  17. }

答案4

得分: 1

是的!我找到了一个解决方案。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Byte [2]byte
  6. //左移
  7. func SL(b Byte) Byte {
  8. if b[0]&0x80 == 0x80 {
  9. b[0] <<= 1
  10. if b[1]&0x80 == 0x80 {
  11. b[0] ^= 1
  12. b[1] <<= 1
  13. } else {
  14. b[1] <<= 1
  15. }
  16. b[1] ^= 0x01
  17. } else {
  18. b[0] <<= 1
  19. if b[1]&0x80 == 0x80 {
  20. b[0] ^= 1
  21. b[1] <<= 1
  22. } else {
  23. b[1] <<= 1
  24. }
  25. }
  26. return b
  27. }
  28. func main() {
  29. b := Byte{0x23, 0x86}
  30. fmt.Printf("旧字节 %#08b %#08b\n", b[0], b[1]) // 00100011 10000110
  31. c := SL(b)
  32. fmt.Printf("新字节 %#08b %#08b", c[0], c[1]) // 01000111 00001100
  33. }
英文:

Yep! I found a solution.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. type Byte [2]byte
  6. //shift left
  7. func SL(b Byte) Byte {
  8. if b[0]&amp;0x80 == 0x80 {
  9. b[0] &lt;&lt;= 1
  10. if b[1]&amp;0x80 == 0x80 {
  11. b[0] ^= 1
  12. b[1] &lt;&lt;= 1
  13. } else {
  14. b[1] &lt;&lt;= 1
  15. }
  16. b[1] ^= 0x01
  17. } else {
  18. b[0] &lt;&lt;= 1
  19. if b[1]&amp;0x80 == 0x80 {
  20. b[0] ^= 1
  21. b[1] &lt;&lt;= 1
  22. } else {
  23. b[1] &lt;&lt;= 1
  24. }
  25. }
  26. return b
  27. }
  28. func main() {
  29. //var b Byte
  30. b := Byte{0x23, 0x86}
  31. fmt.Printf(&quot;old byte %#08b %#08b\n&quot;, b[0], b[1]) // 00100011 10000110
  32. c := SL(b)
  33. fmt.Printf(&quot;new byte %#08b %#08b&quot;, c[0], c[1]) // 01000111 00001100
  34. }

答案5

得分: 1

将qin的答案进行位左移,扩展如下:

  1. func shiftBytesLeft(a []byte, byBits int) (dst []byte) {
  2. n := len(a)
  3. dst = make([]byte, n)
  4. for i := 0; i < n-1; i++ {
  5. dst[i] = a[i] << byBits
  6. dst[i] = (dst[i] & (0xff - byte(byBits))) | (a[i+1] >> (8 - byte(byBits)))
  7. }
  8. dst[n-1] = a[n-1] << byBits
  9. return dst
  10. }
英文:

shift left by multiple bits, expanding qin's answer:

  1. func shiftBytesLeft(a []byte, byBits int) (dst []byte) {
  2. n := len(a)
  3. dst = make([]byte, n)
  4. for i := 0; i &lt; n-1; i++ {
  5. dst[i] = a[i] &lt;&lt; byBits
  6. dst[i] = (dst[i] &amp; (0xff - byte(byBits))) | (a[i+1] &gt;&gt; (8 - byte(byBits)))
  7. }
  8. dst[n-1] = a[n-1] &lt;&lt; byBits
  9. return dst
  10. }

huangapple
  • 本文由 发表于 2015年4月4日 12:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/29442710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定