Number of Mismatching Bits

Given two integers "first" and "second". Find the number of bits that do not match in the binary representation of the given numbers.

For example, let "first" be 11 and "second" be 15. Writing both the given numbers in binary we get first = 1011(binary representation of 11) and second = 1111(binary representation of 15) we can see all the bits are the same except the second bit.

ex

Therefore only 1 bit differs hence we return 1 as shown in the diagram.

Note:
1. Do not print anything, just implement the provided function and return the number of mismatching bits for the given numbers.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The only line of each test case contains 2 space-separated integers "first" and "second"
Output Format
For each test case, print a single integer denoting the number of mismatched bits in their binary representation.

The output of each test case will be printed in a separate line.
Constraints:
1 <= T <= 10 ^ 5
0 <= first, second <= 10^9

Where ‘T’ is the total number of test cases and "first", " second" are the given numbers.

Time limit: 1 second
CodingNinjas
author
2y

Step 1 : I used bitwise shift operators here. I first ran a loop for 'i' from 0 to 32 (as 32 bits is maximum)
Step 2 : Then right shifted the bits of both numbers by 'i' places.
Step 3 : Checked whether...read more

CodingNinjas
author
2y
Brute force Approach

The key idea is to check for each bit if it is different or not.

Consider the following steps:

  1. Create a variable “count” which stores the count of mismatched bits. Initialize it to z...read more
CodingNinjas
author
2y
XOR approach


Since we need to find the number of mismatching bits, we can use the XOR operator as the XOR of two given numbers will give the result 1 in the bits where the bits of the given numbers di...read more

Add answer anonymously...
Wells Fargo Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter