# Problem - (ID 6439) You are required to fix all logical errors in the given code. You can click on Run anytime to check the compilation/execution status of the program. You can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all test cases. Do not write the main() function as it is not required. Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods. The function samePositionBitCount(int N1, int N2) accepts two integers N1 and N2 as the input. The function is supposed to return the count of bits which are equal in both the integers at the same positions. The function compiles fine but fails to return the desired result due to logical errors. Your task is to fix the program so that it passes all test cases. Erroneous Code: int samePositionBitCount(int N1, int N2) { int count = 0; int N3 = N1 ^ N2; int max = N1 > N2 ? N1 : N2; while(N3 > 0) { if((N3 & 1) == 0) { count++; } max /= 2; N3 /= 2; } return count; } Explanation: Let 12 and 8 be the values of N1 and N2. So the value of N3 is N1 XOR N2 as given below, 1100 = 12 -> N1 1000 = 8 -> N2 ---------- 0100 = 4 -> N3 The count of zeroes in N3 gives the count of bits which are equal in the same positions. The loop must run to check all the bits in N3 even if the leftmost bit in N3 (0100) is 0. So the correct condition in the while loop must be, while(max > 0) Solution: int samePositionBitCount(int N1, int N2) { int count = 0; int N3 = N1 ^ N2; int max = N1 > N2 ? N1 : N2; while(max > 0) { if((N3 & 1) == 0) { count++; } max /= 2; N3 /= 2; } return count; }

Same Positions Bit Count - AUTOMATA FIX3 months ago

Problem - (ID 6439)

You are required to fix all logical errors in the given code. You can click on Run anytime to check the compilation/execution status of the program. You can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all test cases. Do not write the main() function as it is not required.

Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods.

The function samePositionBitCount(int N1, int N2) accepts two integers N1 and N2 as the input. The function is supposed to return the count of bits which are equal in both the integers at the same positions.

The function compiles fine but fails to return the desired result due to logical errors.

Your task is to fix the program so that it passes all test cases.

Erroneous Code:

int samePositionBitCount(int N1, int N2)
{
int count = 0;
int N3 = N1 ^ N2;
int max = N1 > N2 ? N1 : N2;
while(N3 > 0)
{
if((N3 & 1) == 0)
{
count++;
}
max /= 2;
N3 /= 2;
}
return count;
}

Explanation:

Let 12 and 8 be the values of N1 and N2. So the value of N3 is N1 XOR N2 as given below,

1100 = 12 -> N1
1000 = 8  -> N2
----------
0100 = 4  -> N3

The count of zeroes in N3 gives the count of bits which are equal in the same positions.
The loop must run to check all the bits in N3 even if the leftmost bit in N3 (0100) is 0.
So the correct condition in the while loop must be,

while(max > 0)

Solution:

int samePositionBitCount(int N1, int N2)
{
int count = 0;
int N3 = N1 ^ N2;
int max = N1 > N2 ? N1 : N2;
while(max > 0)
{
if((N3 & 1) == 0)
{
count++;
}
max /= 2;
N3 /= 2;
}
return count;
}