What is gmp_hamdist in PHP?

gmp_hamdist is a function in PHP that calculates the hamming distance between two numbers.

The hamming distance is the number of bit positions in which the two bits are not the same.

Syntax

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

Parameters

The function takes in two parameters:

  1. num1
  2. num2

Both the parameters should be positive.

num1 and num2 can be a numeric string, GMPGNU Multiple Precision Arithmetic Library object, or of the int type.

Return value

The function returns the hamming distance between num1 and num2. The value returned is of the int type.

Code

<?php
// gmp_init is used to create a GMP number
$val1 = gmp_init("1001010011", 2);
$val2 = gmp_init("1011111100", 2);
$distance = gmp_hamdist($val1, $val2);
echo $distance."\n";
?>

Expected output

6

Free Resources