如何反转一个二进制数?

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

How to reverse a binary number?

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是Golang的新手,对于有经验的Golang开发人员来说,这应该是一个简单的问题。我尝试做来自Spotify的相同测试,看看在Golang中我们能有多快 如何反转一个二进制数?

英文:

I'm newbie in Golan, this should be an easy question for experienced golang devs. I try to do the same test from Spotify to see how fast we can go in Golang 如何反转一个二进制数?

答案1

得分: 6

通常的位操作的 C 解决方案可以直接转换为 Go 语言。

package main

import "fmt"

func BitReverse32(x uint32) uint32 {
    x = (x&0x55555555)<<1 | (x&0xAAAAAAAA)>>1
    x = (x&0x33333333)<<2 | (x&0xCCCCCCCC)>>2
    x = (x&0x0F0F0F0F)<<4 | (x&0xF0F0F0F0)>>4
    x = (x&0x00FF00FF)<<8 | (x&0xFF00FF00)>>8
    return (x&0x0000FFFF)<<16 | (x&0xFFFF0000)>>16
}

func main() {
    cases := []uint32{0x1, 0x100, 0x1000, 0x1000000, 0x10000000, 0x80000000, 0x89abcdef}
    for _, c := range cases {
        fmt.Printf("%08x -> %08x\n", c, BitReverse32(c))
    }
}
英文:

The usual bit-twiddling C solutions translate immediately to Go.

package main

import &quot;fmt&quot;

func BitReverse32(x uint32) uint32 {
	x = (x&amp;0x55555555)&lt;&lt;1 | (x&amp;0xAAAAAAAA)&gt;&gt;1
	x = (x&amp;0x33333333)&lt;&lt;2 | (x&amp;0xCCCCCCCC)&gt;&gt;2
	x = (x&amp;0x0F0F0F0F)&lt;&lt;4 | (x&amp;0xF0F0F0F0)&gt;&gt;4
	x = (x&amp;0x00FF00FF)&lt;&lt;8 | (x&amp;0xFF00FF00)&gt;&gt;8
	return (x&amp;0x0000FFFF)&lt;&lt;16 | (x&amp;0xFFFF0000)&gt;&gt;16
}

func main() {
	cases := []uint32{0x1, 0x100, 0x1000, 0x1000000, 0x10000000, 0x80000000, 0x89abcdef}
	for _, c := range cases {
		fmt.Printf(&quot;%08x -&gt; %08x\n&quot;, c, BitReverse32(c))
	}
}

答案2

得分: 2

注意:自2013年以来,您现在有一个专用的math/bits,它是在Go 1.9(2017年8月)中引入的。

它提供了一系列的Reverse()ReverseBytes()函数:不再需要自己实现了。

此外,在大多数架构上,编译器还会将此包中的函数识别为内部函数,以提供额外的性能。

英文:

Note: since 2013, you now have a dedicate math/bits package with Go 1.9 (August 2017).

And it does come with a collection of Reverse() and ReverseBytes() functions: no need to implement one anymore.

Plus, on most architectures, functions in this package are additionally recognized by the compiler and treated as intrinsics for additional performance.

答案3

得分: 1

最直接的解决方案是使用strconv将位转换为数字,然后通过位移操作将数字反转。我不确定它的速度有多快,但应该可以工作。

package main

import "fmt"
import "strconv"

func main() {
    bits := "10100001"
    bits_number := 8
    number, _ := strconv.ParseUint(bits, 2, bits_number)
    r_number := number - number // reserve type
    for i := 0; i < bits_number; i++ {
        r_number <<= 1
        r_number |= number & 1
        number >>= 1
    }
    fmt.Printf("%s [%d]\n", strconv.FormatUint(r_number, 2), r_number)
}
英文:

The most straight-forward solution would be converting the bits into a number with strconv and then reversing the number by shifting the bits. I'm not sure how fast it would be, but it should work.

package main

import &quot;fmt&quot;
import &quot;strconv&quot;

func main() {
	bits := &quot;10100001&quot;
	bits_number := 8
	number, _ := strconv.ParseUint(bits, 2, bits_number)
	r_number := number - number // reserve type
	for i := 0; i &lt; bits_number; i++ {
		r_number &lt;&lt;= 1
		r_number |= number &amp; 1
		number &gt;&gt;= 1
	}
	fmt.Printf(&quot;%s [%d]\n&quot;, strconv.FormatUint(r_number, 2), r_number)
}

http://play.golang.org/p/YLS5wkY-iv

huangapple
  • 本文由 发表于 2013年8月17日 07:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/18283582.html
匿名

发表评论

匿名网友

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

确定