What is gmp_hamdist in PHP?
gmp_hamdist is a function in PHP that calculates the hamming distance between two numbers.
The hamming distance is the number of bit positions in which the two bits are not the same.
Syntax
gmp_hamdist(GMP|int|string $num1, GMP|int|string $num2): int
Parameters
The function takes in two parameters:
num1num2
Both the parameters should be positive.
num1andnum2can be a numeric string,object, or of the GMP GNU Multiple Precision Arithmetic Library inttype.
Return value
The function returns the hamming distance between num1 and num2. The value returned is of the int type.
Code
<?php// gmp_init is used to create a GMP number$val1 = gmp_init("1001010011", 2);$val2 = gmp_init("1011111100", 2);$distance = gmp_hamdist($val1, $val2);echo $distance."\n";?>
Expected output
6