# Algorithms_in_C++: bit_manipulation/hamming_distance.cpp File Reference

Loading...
Searching...
No Matches
hamming_distance.cpp File Reference

Returns the Hamming distance between two integers. More...

`#include <cassert>`
`#include <iostream>`
Include dependency graph for hamming_distance.cpp:

## Namespaces

namespace | bit_manipulation
| for IO operations
namespace | hamming_distance
| Functions for Hamming distance implementation.

## Functions

uint64_t | bit_manipulation::hamming_distance::bitCount (uint64_t value)
uint64_t | bit_manipulation::hamming_distance::hamming_distance (uint64_t a, uint64_t b)
uint64_t | bit_manipulation::hamming_distance::hamming_distance (const std::string &a, const std::string &b)
static void | test ()
| Function to the test hamming distance. More...
int | main ()
| Main function. More...

## Detailed Description

Returns the Hamming distance between two integers.

To find hamming distance between two integers, we take their xor, which will have a set bit iff those bits differ in the two numbers. Hence, we return the number of such set bits.

Author
  Ravishankar Joshi

## Function Documentation

## bitCount()

uint64_t bit_manipulation::hamming_distance::bitCount | ( | uint64_t | value | ) |

This function returns the number of set bits in the given number.

Parameters

  value | the number of which we want to count the number of set bits.

Returns
  the number of set bits in the given number.

{
uint64_t count = 0;
while (value) { // until all bits are zero
if (value & 1) { // check lower bit
count++;
}
value >>= 1; // shift bits, removing lower bit
}
return count;
}
Here is the call graph for this function:

## hamming_distance() [1/2]

uint64_t bit_manipulation::hamming_distance::hamming_distance | ( | const std::string & | a,
|  | const std::string & | b
| ) |  |

This function returns the hamming distance between two strings.

Parameters

  a | the first string
  b | the second string

Returns
  the number of characters differing between the two strings.

{
assert(a.size() == b.size());
size_t n = a.size();
uint64_t count = 0;
for (size_t i = 0; i < n; i++) {
count += (b[i] != a[i]);
}
return count;
}
std::string::size
T size(T... args)
Here is the call graph for this function:

## hamming_distance() [2/2]

uint64_t bit_manipulation::hamming_distance::hamming_distance | ( | uint64_t | a,
|  | uint64_t | b
| ) |  |

This function returns the hamming distance between two integers.

Parameters

  a | the first number
  b | the second number

Returns
  the number of bits differing between the two integers.

{ return bitCount(a ^ b); }
bit_manipulation::hamming_distance::bitCount
uint64_t bitCount(uint64_t value)
Definition: hamming_distance.cpp:34
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // execute the tests
uint64_t a = 11; // 1011 in binary
uint64_t b = 2; // 0010 in binary
std::cout << "Hamming distance between " << a << " and " << b << " is "
<< bit_manipulation::hamming_distance::hamming_distance(a, b)
<< std::endl;
}
std::cout
std::endl
T endl(T... args)
test
static void test()
Function to the test hamming distance.
Definition: hamming_distance.cpp:75
bit_manipulation::hamming_distance::hamming_distance
uint64_t hamming_distance(uint64_t a, uint64_t b)
Definition: hamming_distance.cpp:51
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Function to the test hamming distance.

Returns
  void

{
assert(bit_manipulation::hamming_distance::hamming_distance(11, 2) == 2);
assert(bit_manipulation::hamming_distance::hamming_distance(2, 0) == 1);
assert(bit_manipulation::hamming_distance::hamming_distance(11, 0) == 3);
assert(bit_manipulation::hamming_distance::hamming_distance("1101",
"1111") == 1);
assert(bit_manipulation::hamming_distance::hamming_distance("1111",
"1111") == 0);
assert(bit_manipulation::hamming_distance::hamming_distance("0000",
"1111") == 4);
assert(bit_manipulation::hamming_distance::hamming_distance("alpha",
"alphb") == 1);
assert(bit_manipulation::hamming_distance::hamming_distance("abcd",
"abcd") == 0);
assert(bit_manipulation::hamming_distance::hamming_distance("dcba",
"abcd") == 4);
}