A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d4/d48/hamming__distance_8cpp.html below:

TheAlgorithms/C++: bit_manipulation/hamming_distance.cpp File Reference

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.

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.

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.

Parameters
a the first string b the 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
a the first number b the second number
Returns
the number of bits differing between the two integers.

Definition at line 52 of file hamming_distance.cpp.

uint64_t bitCount(uint64_t value)

◆ main()

Main function.

Returns
0 on exit

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.

Returns
void

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