比较两个数组中的位数

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

Compare bits in two arrays

问题

我已经翻译好了你的代码,如下所示:

package main

import (
	"crypto/sha256"
	"fmt"
)

var s1 string = "unodostresquatro"
var s2 string = "UNODOSTRESQUATRO"
var h1 = sha256.Sum256([]byte(s1))
var h2 = sha256.Sum256([]byte(s2))

func main() {
	fmt.Printf("s1: %s h1: %X h1 type: %T\n", s1, h1, h1)
	fmt.Printf("s2: %s h2: %X h2 type: %T\n", s2, h2, h2)
	fmt.Printf("不同位的数量: %d\n", DifferentBits(h1, h2))
}

func DifferentBits(c1 [32]uint8, c2 [32]uint8) int {
	var counter int
	for x := range c1 {
		diff := c1[x] ^ c2[x]
		for diff > 0 {
			counter += int(diff & 1)
			diff >>= 1
		}
	}
	return counter
}

这个修改后的代码会计算两个SHA256哈希值中不同位的数量。我在DifferentBits函数中添加了一些代码,用于计算不同位的数量。我使用了异或操作符(^)来比较两个字节,并使用位运算来计算不同位的数量。希望这可以帮到你!

英文:

I've got stuck with ex4.1 for the book which says: <br />
Write a function that counts the number of bits that are different in two SHA256 hashes.

The partial solution I came up with is pasted below, but it's wrong - it counts number of different bytes not bits.
Could you please point me in the right direction?

package main

import (
    &quot;crypto/sha256&quot;
    &quot;fmt&quot;
)

var s1 string = &quot;unodostresquatro&quot;
var s2 string = &quot;UNODOSTRESQUATRO&quot;
var h1 = sha256.Sum256([]byte(s1))
var h2 = sha256.Sum256([]byte(s2))

func main() {
    fmt.Printf(&quot;s1: %s h1: %X h1 type: %T\n&quot;, s1, h1, h1) 
    fmt.Printf(&quot;s2: %s h2: %X h2 type: %T\n&quot;, s2, h2, h2) 
    fmt.Printf(&quot;Number of different bits: %d\n&quot;, 8 * DifferentBits(h1, h2))
}

func DifferentBits(c1 [32]uint8, c2 [32]uint8) int {
    var counter int 
    for x := range c1 {
        if c1[x] != c2[x] {
            counter += 1
        }
    }   
    return counter

}

答案1

得分: 5

《Go编程语言》是Alan A. A. Donovan和Brian W. Kernighan合著的一本书。练习4.1要求编写一个函数,用于计算两个SHA256哈希值中不同的位数。

《C程序设计语言》是Brian W. Kernighan和Dennis M. Ritchie合著的一本书。练习2-9要求利用二进制补码系统中的观察,使用x &= (x-1)来删除x中最右边的1位。利用这个观察,编写一个更快的bitcount版本。

在《位操作技巧》一书中,Sean Eron Anderson介绍了一种计算位集合的位数的方法,即Brian Kernighan的方法。代码示例中,使用一个循环来计算一个无符号整数v中设置的位数。

对于练习4.1,你需要计算不同的位数,而不是字节。例如,给出的代码示例中,通过比较两个SHA256哈希值的每个字节的异或结果,然后计算不同位的数量。最后输出结果为139。

英文:

> The Go Programming Language
>
> Alan A. A. Donovan · Brian W.Kernighan
>
> Exercise 4.1: Write a function that counts the number of bits that
> are different in two SHA256 hashes.


> The C Programming Language
>
> Brian W.Kernighan · Dennis M. Ritchie
>
> Exercise 2-9. In a two's complement number system, x &amp;= (x-1) deletes
> the rightmost 1-bit in x. Use this observation to write a faster
> version of bitcount.


> Bit Twiddling Hacks
>
> Sean Eron Anderson
>
> Counting bits set, Brian Kernighan's way
>
> unsigned int v; // count the number of bits set in v
> unsigned int c; // c accumulates the total bits set in v
> for (c = 0; v; c++)
> {
> v &= v - 1; // clear the least significant bit set
> }


For exercise 4.1, you are counting the number of bytes that are different. Count the number of bits that are different. For example,

package main

import (
	&quot;crypto/sha256&quot;
	&quot;fmt&quot;
)

func BitsDifference(h1, h2 *[sha256.Size]byte) int {
	n := 0
	for i := range h1 {
		for b := h1[i] ^ h2[i]; b != 0; b &amp;= b - 1 {
			n++
		}
	}
	return n
}

func main() {
	s1 := &quot;unodostresquatro&quot;
	s2 := &quot;UNODOSTRESQUATRO&quot;
	h1 := sha256.Sum256([]byte(s1))
	h2 := sha256.Sum256([]byte(s2))
	fmt.Println(BitsDifference(&amp;h1, &amp;h2))
}

Output:

139

答案2

得分: 1

这是我会做的方式

package main

import (
	"crypto/sha256"
	"fmt"
)

var (
	s1 string = "unodostresquatro"
	s2 string = "UNODOSTRESQUATRO"
	h1        = sha256.Sum256([]byte(s1))
	h2        = sha256.Sum256([]byte(s2))
)

func main() {
	fmt.Printf("s1: %s h1: %X h1 type: %T\n", s1, h1, h1)
	fmt.Printf("s2: %s h2: %X h2 type: %T\n", s2, h2, h2)
	fmt.Printf("不同位的数量: %d\n", DifferentBits(h1, h2))
}

// bitCount 计算 x 中设置的位数
func bitCount(x uint8) int {
	count := 0
	for x != 0 {
		x &= x - 1
		count++
	}
	return count
}

func DifferentBits(c1 [32]uint8, c2 [32]uint8) int {
	var counter int
	for x := range c1 {
		counter += bitCount(c1[x] ^ c2[x])
	}
	return counter
}

Playground

英文:

Here is how I would do it

package main

import (
	&quot;crypto/sha256&quot;
	&quot;fmt&quot;
)

var (
	s1 string = &quot;unodostresquatro&quot;
	s2 string = &quot;UNODOSTRESQUATRO&quot;
	h1        = sha256.Sum256([]byte(s1))
	h2        = sha256.Sum256([]byte(s2))
)

func main() {
	fmt.Printf(&quot;s1: %s h1: %X h1 type: %T\n&quot;, s1, h1, h1)
	fmt.Printf(&quot;s2: %s h2: %X h2 type: %T\n&quot;, s2, h2, h2)
	fmt.Printf(&quot;Number of different bits: %d\n&quot;, DifferentBits(h1, h2))
}

// bitCount counts the number of bits set in x
func bitCount(x uint8) int {
	count := 0
	for x != 0 {
		x &amp;= x - 1
		count++
	}
	return count
}

func DifferentBits(c1 [32]uint8, c2 [32]uint8) int {
	var counter int
	for x := range c1 {
		counter += bitCount(c1[x] ^ c2[x])
	}
	return counter
}

Playground

huangapple
  • 本文由 发表于 2016年1月24日 01:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/34966658.html
匿名

发表评论

匿名网友

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

确定