Question
Please help in this problem, Hamming distance printing as 0 for any 2 numbers I put in which I feel is wrong, please check my
Please help in this problem, Hamming distance printing as 0 for any 2 numbers I put in which I feel is wrong, please check my code in C.. The main function shouldn't be changed, but it's the last 6 lines of code which has the error, where I ask user to input two numbers.
#include
int main(int *argc, char **argv) { uint32_t Number; uint32_t bitPosition; uint32_t value; int numOnes; uint32_t input1; uint32_t input2; uint32_t hDist;
value = 1; bitPosition = 21; Number = 15345;
numOnes = countNumberofOnes(&Number);
setBit(&Number, bitPosition,value); // set bit in bitposition to value
hDist = hammingDistance(input1, input2); // Calculates hamming distance printf(" Hamming Distance = %d \t number of Ones : %d ", hDist, numOnes);
return 0; }
#include "BitManipulations.h"
int countNumberofOnes(uint32_t *intData) { //Set the initial value.
int count = 0;
//Store the number in temp.
uint32_t temp = *intData;
//Start the loop until temp is not 0.
while (temp != 0)
{
//Compute the bit wise AND operation.
temp = temp&(temp - 1);
//Increment the value of count.
count++;
}
//Return the value of count.
return count;
} //* setBit //* //* Description: The function sets the bit in the specified bit position in an to the specifid value. //* Preconditions: Value can be a 1 or 0. bitPosition will be between 0 and 31 (for integer size argument) //* Postconditions: The bit of *inData at position biPosition will be set to value
void setBit(uint32_t *inData, uint32_t bitPosition,uint32_t value) { uint32_t d = 1; d = d << bitPosition; d = ~d; uint32_t temp = *inData; temp = temp & d; value = value << bitPosition; temp = temp | value; *inData = temp; } //* 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
int hammingDistance(uint32_t inData1, uint32_t inData2) {
int res=0,i; uint32_t temp1 = inData1; uint32_t temp2 = inData2; uint32_t d = 1; for(i = 0;i<32;i++){ if((temp1 & (d<
printf(" Enter the first number: ");
scanf("%u", &temp1);
printf("Enter the second number: ");
scanf("%u", &temp2); return res;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started