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 // 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" ; ?> |
Output:
4 12
Program 2: Program to calculate the hamming distance when GMP numbers are passed as arguments.
<?php // 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" ; ?> |
Output:
1 0
Reference:
http://php.net/manual/en/function.gmp-hamdist.php
<!–
–>
Please Login to comment…