Number of Mismatching Bits in JavaIn this tutorial, we are going to discuss the problem number of mismatching bits in Java. In this problem, two numbers (f1 and f2) are given to us. Our task is to find the number of mismatching bits when we compare the binary representation of both numbers. Assume that every number fits in 8 bits. Example 1: Input int f1 = 9 int f2 = 15 Output The total number of mismatching bits is 2. Explanation: The binary representation of the number 9 in 8 bits is 00001001, and the binary representation of the number 15 in 8 bits is 00001111. If we compare both binary representations from left to right, we find that the first five bits match. The sixth and seventh bits do not match as both the bits are zero for the number 9 and 1 for the number 15. Hence, there are two bits that do not match. Therefore, the output is 2. Example 2: Input int f1 = 32 int f2 = 31 Output The total number of mismatching bits is 6. Explanation: The binary representation of the number 32 in 8 bits is 00100000, and the binary representation of the number 31 in 8 bits is 00011111. If we compare both binary representations from left to right, we find that the first two bits match. After that, there is a mismatch till the eighth bit. The third bit is 1 for the number 32 and 0 for the number 31, and for the rest of the other bits, the value is 0 for the number 32 and 1 for the number 31. Hence, there are 6 bits that do not match. Therefore, the output is 6. Example 3: Input int f1 = 40 int f2 = 40 Output The total number of mismatching bits is 0. Explanation: Since both numbers are the same, their binary representation will also be the same. Hence, there will be zero mismatching bits. Therefore, the output is 0. Simple ApproachThe simple approach is to first convert both the numbers (f1 and f2) into their binary representation. Then store their binary representation in array lists (one for the binary representation of f1 and the other for the binary representation of f2). After that, using a loop, we can compare the bits and find out the answer. Observe the following program. FileName: MismatchingBits.java Output: The number of mismatching bits for the numbers 9 and 15 is: 2 The number of mismatching bits for the numbers 32 and 31 is: 6 The number of mismatching bits for the numbers 40 and 40 is: 0 Complexity Analysis: There are four while loops and one for-loop used in the program. In all of these five loops, the two while loops and the one for loop will only run for a constant time. The last two while loops and the for loops take O(8) time in the worst-case scenario, which is constant. So, the overall time complexity of the program depends on the first two while-loops. The first while-loop takes O(dig1) time and the second loop takes O(dig2) time. Thus, the overall time complexity of the program is O(dig1 + dig2), where dig1 is the total number of digits present in the number f1, and dig2 is the total number of digits present in the number f2. The space complexity of the program takes O(8) times, which is constant. We can avoid the use of array lists for storing the bits of the given number. The following program shows the same. FileName: MisMatchBits1.java Output: The number of mismatching bits for the numbers 9 and 15 is: 2 The number of mismatching bits for the numbers 32 and 31 is: 6 The number of mismatching bits for the numbers 40 and 40 is: 0 Complexity Analysis: The program uses only one loop that runs only O(8) times which is constant. Also, the program is not using any extra space. Thus, the time, as well as the space complexity of the program, is O(1). Using XOR OperatorAnother way to find the number of mismatching bits is to use bitwise XOR. Whenever we apply bitwise XOR on two numbers, we get a number whose those bits are set that are not matching in the two numbers. Now, compute the number of set bits in the XOR value. That computed value is our answer. Let's take an example to understand this. We take two numbers, 10 and 2. Now, 10 ^ 2 = 8. It is because 1010 ^ 0010 = 1000 (value is 8). We see that the first bit (starting from right to left) is set to 1 as the first bit is 1 for number 10 and 0 for number 2 (a mismatch). The rest of the other bits match both numbers. Hence, the rest of the bits are 0 in the XOR value. In the XOR value, we see that only one bit is set. Hence, the number of mismatched bits for the numbers 10 and 2 is 1. Observe the following program. FileName: MismatchingBits2.java Output: The number of mismatching bits for the numbers 9 and 15 is: 2 The number of mismatching bits for the numbers 32 and 31 is: 6 The number of mismatching bits for the numbers 40 and 40 is: 0 Complexity Analysis: The time, as well as the space complexity of the program, is constant, i.e., O(1). Note: We have discussed three programs in this tutorial, and asymptotically the time complexity of all the programs is the same. However, of all these three programs, the third program is the best in terms of complexity. It is because the while-loop used in the third program will run only O(setBits) times, where setBits is the number of set bits in the XOR value of the two given numbers. Usually, the number of set bits are quite less in an XOR value. Hence, in most cases, the while-loop of the third program will run even less than O(8) times. |