Given two integers A and B, the task is to count the number of bits needed to be flipped to convert A to B.
Input: A = 10, B = 7
binary(10) = 1010
binary(7) = 0111
3 bits need to be flipped.
Input: A = 8, B = 7
Approach: An approach to solve this problem has already been discussed here. Here, the count of bits that need to be flipped can be found by matching all the bits in both the integers one by one. If the bit under consideration differs then increment the count.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count number of bits to be flipped to convert A to B
- Count of total bits toggled/flipped in binary representation of 0 to N
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Check if bits of a number has count of consecutive set bits in increasing order
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Count Set-bits of number using Recursion
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count total bits in a number
- Count unset bits of a number
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count number of bits changed after adding 1 to given N
- Program to count number of set bits in an (big) array
- Count of pairs in an Array with same number of set bits
- Count number of set bits in a range using bitset
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Check if the number is valid when flipped upside down
- Toggle bits of a number except first and last bits
- Count set bits in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.