• PHP Video Tutorials

PHP - gmp_hamdist() Function



Definition and Usage

The gmp_hamdist() function calculates the hamming distance between two given numbers.

Description

Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different. The Hamming distance between two strings, a and b is denoted as d(a,b). The numbers used to calculate has to be positive.

Syntax

gmp_hamdist ( GMP $a , GMP $b ) : int

Parameters

Sr.No Parameter & Description
1

a

It can a GMP resource number , a gmp object or a numeric string.

2

b

It can a GMP resource number , a gmp object or a numeric string.

Return Values

PHP gmp_hamdist() function returns hamming distance of two given positive numbers and the output is a GMP number.

PHP Version

This function will work from PHP Version greater than 5.0.0.

Example 1

Working of gmp_hamdist −

<?php
   $ham1 = gmp_init("11011001", 2);
   $ham2 = gmp_init("10011101", 2);
   echo "The hamming distance d(11011001, 10011101) is :".gmp_hamdist($ham1, $ham2);
?>

This will produce following result −

The hamming distance d(11011001, 10011101) is :2

Example 2

Working of gmp_hamdist −

<?php
   $ham1 = gmp_init("010", 2);
   $ham2 = gmp_init("111", 2);
   echo "The hamming distance d(010, 111) is :".gmp_hamdist($ham1, $ham2);
?>

This will produce following result −

The hamming distance d(010, 111) is :2
php_function_reference.htm
Advertisements