Given two numbers, the task is to use alternative bits within two numbers to create result. We take first bits of second number, then second bit of the first number, third bit of second number and take the fourth bit of a first number and so on and generate a number with it.
Input : n = 10, m = 11 Output : 11 Start from right of second number Binary representation of n = 1 0 1 0 ^ ^ Binary representation of m = 1 0 1 1 ^ ^ Output is = 1 0 1 1 Input : n = 20, m = 7 Output : 5 Start from right of second number binary representation of n = 1 0 1 0 0 ^ ^ binary representation of m = 0 0 1 1 1 ^ ^ ^ Output is = 0 0 1 0 1
1. Get the set even bits number of n.
2. Get the set odd bits number of m.
3. return OR of these number.
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.
- Print numbers in the range 1 to n having bits in alternate pattern
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether bits are in alternate pattern in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Maximum sum by adding numbers with same number of set bits
- Closest (or Next) smaller and greater numbers with same number of set bits
- Print numbers having first and last bits as the only set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Sum of numbers with exactly 2 bits set
- Print first n numbers with exactly two set bits
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Count total unset bits in all the numbers from 1 to N
- Count total set bits in all numbers from range L to R
- Check if bits in range L to R of two numbers are complement of each other or not
- Python map function | Count total set bits in all numbers from 1 to n
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
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.
Improved By : Mithun Kumar