Loading...
Searching...
No Matches
Returns the Hamming distance between two integers. More...
#include <cassert>
#include <cstdint>
#include <iostream>
Go to the source code of this file.
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.
Definition in file hamming_distance.cpp.
◆ bitCount() uint64_t bit_manipulation::hamming_distance::bitCount ( uint64_t value )This function 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) {
38 if (value & 1) {
39 count++;
40 }
41 value >>= 1;
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.
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.
Definition at line 52 of file hamming_distance.cpp.
uint64_t bitCount(uint64_t value)
◆ main()Main function.
Definition at line 100 of file hamming_distance.cpp.
100 {
102 uint64_t a = 11;
103 uint64_t b = 2;
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()Function to the test hamming distance.
Definition at line 76 of file hamming_distance.cpp.
76 {
80
82 "1111") == 1);
84 "1111") == 0);
86 "1111") == 4);
87
89 "alphb") == 1);
91 "abcd") == 0);
93 "abcd") == 4);
94}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4