PHP | gmp_hamdist() Function (original) (raw)

Last Updated : 11 Jul, 2025

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,num1, 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 num1andnum1 and num1andnum2. 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",a = "3", 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",a = "4", 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 `

hamDist=gmphamdist(hamDist = gmp_hamdist(hamDist=gmphamdist(a, $b); echo $hamDist."\n"; // calculates the hamming distance a="4";a = "4"; a="4";b = "4"; hamDist=gmphamdist(hamDist = gmp_hamdist(hamDist=gmphamdist(a, $b); echo $hamDist."\n"; ?>

`

Output:

4 12

Program 2: Program to calculate the hamming distance when GMP numbers are passed as arguments.

php `

hamDist=gmphamdist(hamDist = gmp_hamdist(hamDist=gmphamdist(a, $b); echo $hamDist."\n"; // calculates the hamming distance $a = gmp_init("100", 2); b=gmpinit("100",2);<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>m</mi><mi>D</mi><mi>i</mi><mi>s</mi><mi>t</mi><mo>=</mo><mi>g</mi><mi>m</mi><msub><mi>p</mi><mi>h</mi></msub><mi>a</mi><mi>m</mi><mi>d</mi><mi>i</mi><mi>s</mi><mi>t</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">hamDist=gmphamdist(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">ham</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">D</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mordmathnormal">m</span><spanclass="mord"><spanclass="mordmathnormal">p</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.3361em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">h</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">am</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span></span></span></span>a,b = gmp_init("100", 2); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>m</mi><mi>D</mi><mi>i</mi><mi>s</mi><mi>t</mi><mo>=</mo><mi>g</mi><mi>m</mi><msub><mi>p</mi><mi>h</mi></msub><mi>a</mi><mi>m</mi><mi>d</mi><mi>i</mi><mi>s</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">hamDist = gmp_hamdist(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">ham</span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">m</span><span class="mord"><span class="mord mathnormal">p</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">h</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">am</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>a, b=gmpinit("100",2);<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>m</mi><mi>D</mi><mi>i</mi><mi>s</mi><mi>t</mi><mo>=</mo><mi>g</mi><mi>m</mi><msub><mi>p</mi><mi>h</mi></msub><mi>a</mi><mi>m</mi><mi>d</mi><mi>i</mi><mi>s</mi><mi>t</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">hamDist=gmphamdist(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">ham</span><spanclass="mordmathnormal"style="marginright:0.02778em;">D</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal"style="marginright:0.03588em;">g</span><spanclass="mordmathnormal">m</span><spanclass="mord"><spanclass="mordmathnormal">p</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.3361em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">h</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">am</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span></span></span></span>a,b); echo $hamDist."\n"; ?>

`

Output:

1 0

Reference: https://www.php.net/manual/en/function.gmp-hamdist.php