TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
hamming_distance.cpp File Reference

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

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

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for assert
 
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.
 
int main ()
 Main function.
 

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

Definition in file hamming_distance.cpp.

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
valuethe number of which we want to count the number of set bits.
Returns
the number of set bits in the given number.

Definition at line 35 of file hamming_distance.cpp.

35 {
36 uint64_t count = 0;
37 while (value) { // until all bits are zero
38 if (value & 1) { // check lower bit
39 count++;
40 }
41 value >>= 1; // shift bits, removing lower bit
42 }
43 return count;
44}

◆ 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
athe first string
bthe second string
Returns
the number of characters differing between the two strings.

Definition at line 60 of file hamming_distance.cpp.

60 {
61 assert(a.size() == b.size());
62 size_t n = a.size();
63 uint64_t count = 0;
64 for (size_t i = 0; i < n; i++) {
65 count += (b[i] != a[i]);
66 }
67 return count;
68}

◆ 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
athe first number
bthe second number
Returns
the number of bits differing between the two integers.

Definition at line 52 of file hamming_distance.cpp.

52{ return bitCount(a ^ b); }

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 100 of file hamming_distance.cpp.

100 {
101 test(); // execute the tests
102 uint64_t a = 11; // 1011 in binary
103 uint64_t b = 2; // 0010 in binary
104
105 std::cout << "Hamming distance between " << a << " and " << b << " is "
107 << std::endl;
108}
static void test()
Function to the test hamming distance.
uint64_t hamming_distance(uint64_t a, uint64_t b)

◆ test()

static void test ( )
static

Function to the test hamming distance.

Returns
void

Definition at line 76 of file hamming_distance.cpp.

76 {
77 assert(bit_manipulation::hamming_distance::hamming_distance(11, 2) == 2);
78 assert(bit_manipulation::hamming_distance::hamming_distance(2, 0) == 1);
79 assert(bit_manipulation::hamming_distance::hamming_distance(11, 0) == 3);
80
81 assert(bit_manipulation::hamming_distance::hamming_distance("1101",
82 "1111") == 1);
83 assert(bit_manipulation::hamming_distance::hamming_distance("1111",
84 "1111") == 0);
85 assert(bit_manipulation::hamming_distance::hamming_distance("0000",
86 "1111") == 4);
87
88 assert(bit_manipulation::hamming_distance::hamming_distance("alpha",
89 "alphb") == 1);
90 assert(bit_manipulation::hamming_distance::hamming_distance("abcd",
91 "abcd") == 0);
92 assert(bit_manipulation::hamming_distance::hamming_distance("dcba",
93 "abcd") == 4);
94}