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

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

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 (
	&quot;fmt&quot;
)

type Byte byte

func SL(b Byte) Byte {
	if b&amp;0x80 == 0x80 {
		b &lt;&lt;= 1
		b ^= 0x01
	} else {
		b &lt;&lt;= 1
	}
	return b
}

func main() {
	var b Byte
	b = 0xD3
	fmt.Printf(&quot;old byte %#08b\n&quot;, b) // 11010011
	c := SL(b)
	fmt.Printf(&quot;new byte %#08b&quot;, 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])&lt;&lt;8 | int(v[1])
	x &lt;&lt;= 1
	v[0] = byte(x &gt;&gt; 8)
	v[1] = byte((x &amp; 0xff) | x&gt;&gt;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 &lt; n-1; i++ {
		dst[i] = a[i] &lt;&lt; 1
		dst[i] = (dst[i] &amp; 0xfe) | (a[i+1] &gt;&gt; 7)
	}
	dst[n-1] = a[n-1] &lt;&lt; 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 &lt; 0 {
		bits = -bits
		for i := n - 1; i &gt; 0; i-- {
			data[i] = data[i]&gt;&gt;bits | data[i-1]&lt;&lt;(8-bits)
		}
		data[0] &gt;&gt;= bits
	} else {
		for i := 0; i &lt; n-1; i++ {
			data[i] = data[i]&lt;&lt;bits | data[i+1]&gt;&gt;(8-bits)
		}
		data[n-1] &lt;&lt;= 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 (
	&quot;fmt&quot;
)

type Byte [2]byte

//shift left
func SL(b Byte) Byte {
	if b[0]&amp;0x80 == 0x80 {
		b[0] &lt;&lt;= 1
		if b[1]&amp;0x80 == 0x80 {
			b[0] ^= 1
			b[1] &lt;&lt;= 1
		} else {
			b[1] &lt;&lt;= 1
		}
		b[1] ^= 0x01
	} else {
		b[0] &lt;&lt;= 1
		if b[1]&amp;0x80 == 0x80 {
			b[0] ^= 1
			b[1] &lt;&lt;= 1
		} else {
			b[1] &lt;&lt;= 1
		}
	}
	return b
}

func main() {
	//var b Byte
	b := Byte{0x23, 0x86}
	fmt.Printf(&quot;old byte %#08b %#08b\n&quot;, b[0], b[1]) // 00100011 10000110
	c := SL(b)
	fmt.Printf(&quot;new byte %#08b %#08b&quot;, 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 &lt; n-1; i++ {
        dst[i] = a[i] &lt;&lt; byBits
        dst[i] = (dst[i] &amp; (0xff - byte(byBits))) | (a[i+1] &gt;&gt; (8 - byte(byBits)))
    }
    dst[n-1] = a[n-1] &lt;&lt; byBits
    return dst
}

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:

确定