PHP中如何计算两个二进制序列的汉明距离?

发布于 2024-08-29 17:57:32 字数 101 浏览 5 评论 0原文

hamming('10101010','01010101')

上面的结果应该是8

如何实施?

hamming('10101010','01010101')

The result of the above should be 8.

How to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

远山浅 2024-09-05 17:57:32

无需安装 GMP,这里是任何相同长度的二进制字符串的简单解决方案

function HammingDistance($bin1, $bin2) {
    $a1 = str_split($bin1);
    $a2 = str_split($bin2);
    $dh = 0;
    for ($i = 0; $i < count($a1); $i++) 
        if($a1[$i] != $a2[$i]) $dh++;
    return $dh;
}

echo HammingDistance('10101010','01010101'); //returns 8

without installed GMP here is easy solution for any same-length binary strings

function HammingDistance($bin1, $bin2) {
    $a1 = str_split($bin1);
    $a2 = str_split($bin2);
    $dh = 0;
    for ($i = 0; $i < count($a1); $i++) 
        if($a1[$i] != $a2[$i]) $dh++;
    return $dh;
}

echo HammingDistance('10101010','01010101'); //returns 8
々眼睛长脚气 2024-09-05 17:57:32

您不需要实现它,因为它已经存在:
http://php.net/manual/en/function.gmp-hamdist。 php

(如果您有 GMP 支持)

You don't need to implement it because it already exists:
http://php.net/manual/en/function.gmp-hamdist.php

(If you have GMP support)

故事灯 2024-09-05 17:57:32

以下函数适用于长度超过 32 位的十六进制字符串(等长)。

function hamming($hash1, $hash2) {
        $dh = 0;

        $len1 = strlen($hash1);
        $len2 = strlen($hash2);
        $len = 0;

        do {
            $h1 = hexdec(substr($hash1, $len, 8));
            $h2 = hexdec(substr($hash2, $len, 8));
            $len += 8;
            for ($i = 0; $i < 32; $i++) {
                $k = (1 << $i);
                if (($h1 & $k) !== ($h2 & $k)) {
                    $dh++;
                }
            }
        } while ($len < $len1);

        return $dh;
    }

The following function works with hex strings (equal length), longer than 32 bits.

function hamming($hash1, $hash2) {
        $dh = 0;

        $len1 = strlen($hash1);
        $len2 = strlen($hash2);
        $len = 0;

        do {
            $h1 = hexdec(substr($hash1, $len, 8));
            $h2 = hexdec(substr($hash2, $len, 8));
            $len += 8;
            for ($i = 0; $i < 32; $i++) {
                $k = (1 << $i);
                if (($h1 & $k) !== ($h2 & $k)) {
                    $dh++;
                }
            }
        } while ($len < $len1);

        return $dh;
    }
や三分注定 2024-09-05 17:57:32

如果您没有 GMP 支持,总会出现这样的情况。缺点是它只适用于长度不超过 32 位的二进制字符串。

function hamdist($x, $y){
  for($dist = 0, $val = $x ^ $y; $val; ++$dist){ 
      $val &= $val - 1;
  }
  return $dist;
}

function hamdist_str($x, $y){
    return hamdist(bindec($x), bindec($y));
}


echo hamdist_str('10101010','01010101'); //8

If you don't have GMP support there is always something like this. Downside it only works on binary strings up to 32 bits in length.

function hamdist($x, $y){
  for($dist = 0, $val = $x ^ $y; $val; ++$dist){ 
      $val &= $val - 1;
  }
  return $dist;
}

function hamdist_str($x, $y){
    return hamdist(bindec($x), bindec($y));
}


echo hamdist_str('10101010','01010101'); //8
你曾走过我的故事 2024-09-05 17:57:32

试试这个功能:

function hamming($b1, $b2) {
    $b1 = ltrim($b1, '0');
    $b2 = ltrim($b2, '0');
    $l1 = strlen($b1);
    $l2 = strlen($b2);
    $n = min($l1, $l2);
    $d = max($l1, $l2) - $n;
    for ($i=0; $i<$n; ++$i) {
        if ($b1[$l1-$i] != $b2[$l2-$i]) {
            ++$d;
        }
    }
    return $d;
}

Try this function:

function hamming($b1, $b2) {
    $b1 = ltrim($b1, '0');
    $b2 = ltrim($b2, '0');
    $l1 = strlen($b1);
    $l2 = strlen($b2);
    $n = min($l1, $l2);
    $d = max($l1, $l2) - $n;
    for ($i=0; $i<$n; ++$i) {
        if ($b1[$l1-$i] != $b2[$l2-$i]) {
            ++$d;
        }
    }
    return $d;
}
再见回来 2024-09-05 17:57:32

您可以借助 substr_count( )中提供的代码PHP 手册上的这条评论

/* hamdist is equivilent to: */
echo gmp_popcount(gmp_xor($ham1, $ham2)) . "\n";

You can easily code your hamming function with the help of substr_count() and the code provided in this comment on the PHP manual.

/* hamdist is equivilent to: */
echo gmp_popcount(gmp_xor($ham1, $ham2)) . "\n";
后来的我们 2024-09-05 17:57:32

尝试:

echo gmp_hamdist('10101010','01010101')

Try:

echo gmp_hamdist('10101010','01010101')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文