PHP | gmp_hamdist() Function
The gmp_hamdist() is a built-in function in PHP which is used to find the hamming distance between two GMP numbers (GNU Multiple Precision : For large numbers).
Hamming distance between two numbers is defined as number of mis-matching bits in their binary representation.
Syntax:
gmp_hamdist ( $num1, $num2)
Parameters: This function accepts two GMP numbers $num1 and $num2 as shown in the above syntax. Both of these parameters are mandatory to be passed and must be positive. This function finds the hamming distance between the two numbers $num1 and $num2. These parameters can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
Return Value: This function returns a GMP number which is the calculated hamming distance of the two numbers passed to it as arguments.
Examples:
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
Below programs illustrate the gmp_hamdist() function in PHP :
Program 1: Program to calculate the hamming distance when numeric strings as GMP numbers are passed as arguments.
<?php
$a = "3" ;
$b = "11" ;
$hamDist = gmp_hamdist( $a , $b );
echo $hamDist . "\n" ;
$a = "4" ; $b = "4" ;
$hamDist = gmp_hamdist( $a , $b );
echo $hamDist . "\n" ;
?>
|
Output:
4
12
Program 2: Program to calculate the hamming distance when GMP numbers are passed as arguments.
<?php
$a = gmp_init( "11" , 2);
$b = gmp_init( "1011" , 2);
$hamDist = gmp_hamdist( $a , $b );
echo $hamDist . "\n" ;
$a = gmp_init( "100" , 2);
$b = gmp_init( "100" , 2);
$hamDist = gmp_hamdist( $a , $b );
echo $hamDist . "\n" ;
?>
|
Output:
1
0
Reference:
http://php.net/manual/en/function.gmp-hamdist.php
Last Updated :
20 Apr, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...