gmp_hamdist()是PHP中的内置函数,用于查找两个GMP数字之间的汉明距离(GNU多精度:适用于大数)。
两个数字之间的汉明距离定义为二进制表示的mis-matching位的数量。
用法:
gmp_hamdist ( $num1, $num2)
参数:此函数接受两个GMP数字$num1和$num2,如上面的语法所示。这两个参数都是强制传递的,并且必须为正。此函数查找两个数字$num1和$num2之间的汉明距离。这些参数可以是PHP 5.6和更高版本中的GMP对象,或者也可以传递数字字符串,只要可以将该字符串转换为数字即可。
返回值:此函数返回一个GMP数字,该数字是作为参数传递给它的两个数字的计算汉明距离。
例子:
Input: $a = "3", $b = "11"
Output: 1
Explanation: Binary representation of 3 is 0011
Binary representation of 11 is 1011. So, they
differ by only 1 bit.
Input: $a = "4", $b = "4"
Output: 0
以下示例程序旨在说明PHP中的gmp_hamdist()函数:
程序1:程序将数字字符串作为GMP数字作为参数传递时计算汉明距离。
// PHP program to calculate hamming distance
// strings as GMP numbers
$a = "3";
$b = "11";
// calculates the hamming distance
$hamDist = gmp_hamdist($a, $b);
echo $hamDist."\n";
// calculates the hamming distance
$a = "4"; $b = "4";
$hamDist = gmp_hamdist($a, $b);
echo $hamDist."\n";
?>
输出:
4
12
程序2:程序将GMP编号作为参数传递时计算汉明距离。
// PHP program to calculate hamming distance
// creating GMP numbers using gmp_init()
$a = gmp_init("11", 2); // 3 in decimal
$b = gmp_init("1011", 2); // 11 in decimal
// calculates the hamming distance
$hamDist = gmp_hamdist($a, $b);
echo $hamDist."\n";
// calculates the hamming distance
$a = gmp_init("100", 2);
$b = gmp_init("100", 2);
$hamDist = gmp_hamdist($a, $b);
echo $hamDist."\n";
?>
输出:
1
0