Compare and Identify the location and value at particular position and print it

75 views Asked by At

I got stuck in solving the problem as shown below

Suppose I have two Variable A=10010101 [Correct Bit Value] & B=11001010 [Error Bit value which needs to be compared with A]

The above two variables are eight bit, I need to compare the value at each position from most significant bit to the last bit. What I need is to print the position where they are not identical and what should be the correct/error value for that position.

Example: For B, second position bit value should be '0' when we compared to bit value at position 2nd for A.

I tried to use XOR operation but in that case i didn't find the correct value at that position. Also i want to let you know that Bit A value is fixed and bit B value comes dynamically form a device.

Thanks for your valuable time.

2

There are 2 answers

3
MayurK On BEST ANSWER

Just compare bits and if they are different, print the index and corresponding bit from A.

for(i=0 to sizeof(A))
{
    tempA = A & (1 << i); //Get ith bit of A
    tempB = B & (1 << i); //Get ith bit of B
    if(tempA != tempB ) //Check if they are same.
        printf(i, tempA); //If they are different, print position and correct value.
}
1
Raskayu On

If I get you right, your logic should be like this:

ForEach elem in A 
    C[elem.index] = A XOR B

ForEach elem in C
    if elem == 1 
        print elem.index