//******************************************************************************
ID: 3668024 • Letter: #
Question
//***********************************************************************************/
//* hammingDistance
//* Description: Function hammingDistance calculates total number of bits
//* that need to be inverted in order to change inData1 into inData2 or vice versa.
//* Preconditions: The function accepts two unsigned integers as input
//* Postconditions: The function returns the hamming distance
//*
// Calls: N/A
// Called by: main
//*
//***********************************************************************************/
int hammingDistance(uint32_t inData1, unint32_t inData2)
{
}
Explanation / Answer
int hammingDistance(uint32_t inData1, uint32_t inData2)
{
//this code is modified from this url http://e...content-available-to-author-only...a.org/wiki/Hamming_distance
int dist;
unsigned val;
dist = 0;
val = inData1 ^ inData2; // leaves only what is different
// Count the number of bits set
while (val != 0)
{
// A bit is set, so increment the count and clear the bit
dist++;
val &= val - 1;
}
// returns the number of different bits
return dist;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.