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 // 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
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Map first() Function
- PHP | end() Function
- D3.js | d3.rgb() Function
- p5.js | hex() function
- PHP | pow( ) Function
- PHP | Ds\Map last() Function
- D3.js | d3.hcl() Function
- CSS | hsl() Function
- CSS | var() Function
- PHP | next() Function
- PHP | each() Function
- PHP | Ds\Set xor() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



