gmp_hamdist


gmp apg

RETURNS the Hamming distance between two GMP numbers.

Hamming distance - from Wikipedia 






In this function must be $num1 > 0 and $num2 > 0.



<?php

int gmp_hamdist 
( GMP|int|string $num1, 
                              
GMP|int|string $num2 )


where,

$num1 = The first GMP number

$num2 
= The second GMP number

?>
 

$num1


The first GMP number.



$num2


The second GMP number.



  1 EXERCISE   

<?php

$a01 
= '0x253';
$bin01a = gmp_init(decbin(hexdec($a01)), 2);

$b01 = '0x2fc';
$bin01b = gmp_init(decbin(hexdec($b01)), 2);

$h01a = gmp_hamdist($a01, $b01);

echo 
'gmp_hamdist( ' . $a01 . ', ' . $b01 . ' ) = ' . 
                                   
$h01a . '<br><br>or yet<br><br>';

$h01b = gmp_hamdist($bin01a, $bin01b);

echo 
'gmp_hamdist( ' . $bin01a . ', ' . $bin01b . ' ) = ' . 
                                                     
$h01b . '<br><br>';

echo 
$a01 . ' = ' . $bin01a . '<br>';

echo 
$b01 . ' = ' .$bin01b . '<br><br>';

echo 
'The same as:<br><br>';

$h01c = gmp_popcount(gmp_xor($a01, $b01));

echo 
'gmp_popcount(gmp_xor( ' . $a01 . ', ' . $b01 . ' )) = ' . 
                                                  
$h01c . '<br><br>or<br><br>';

$h01d = gmp_popcount(gmp_xor($bin01a, $bin01b));

echo 
'gmp_popcount(gmp_xor( ' . $bin01a . ', ' . $bin01b . ' )) = ' . 
                                                  
$h01d . '<br>';

?> 

  2 EXERCISE   

<?php

echo "gmp_strval(gmp_hamdist(\"111111\", \"2222222\")) = " . 
               
gmp_strval(gmp_hamdist("111111", "2222222")) . '<br><br>';

echo 
"gmp_strval(gmp_hamdist(123123, 435234)) = " . 
               
gmp_strval(gmp_hamdist(123123, 435234)) . '<br><br>';

echo 
"gmp_strval(gmp_hamdist(555, \"2342341123\")) = " . 
               
gmp_strval(gmp_hamdist(555, "2342341123")) . '<br><br>';

echo 
"gmp_strval(gmp_hamdist(-1, 3333)) = " . 
               
gmp_strval(gmp_hamdist(-1, 3333)) . '<br><br>';

echo 
"gmp_strval(gmp_hamdist(4545, -20)) = " . 
               
gmp_strval(gmp_hamdist(4545, -20))  . '<br><br>';

try {
    
var_dump(gmp_strval(gmp_hamdist("test", "no test")));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

$n = gmp_init("987657876543456");
var_dump(gmp_strval(gmp_hamdist($n, "34332")));
$n1 = gmp_init("987657878765436543456");
var_dump(gmp_strval(gmp_hamdist($n, $n1)));


try {
    
var_dump(gmp_hamdist(array(), 1));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_hamdist(1, array()));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_hamdist(array(), array()));
} catch (
\TypeError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

?>