Given two non-negative long integers, x and y given x <= y, the task is to find bit-wise and of all integers from x and y, i.e., we need to compute value of x & (x+1) & … & (y-1) & y.7
Input : x = 12, y = 15 Output : 12 12 & 13 & 14 & 14 = 12 Input : x = 10, y = 20 Output : 0
A simple solution is to traverse all numbers from x to y and do bit-wise and of all numbers in range.
An efficient solution is to follow following steps.
1) Find position of Most Significant Bit (MSB) in both numbers.
2) If positions of MSB are different, then result is 0.
3) If positions are same. Let positions be msb_p.
……a) We add 2msb_p to result.
……b) We subtract 2msb_p from x and y,
……c) Repeat steps 1, 2 and 3 for new values of x and y.
Example 1 : x = 10, y = 20 Result is initially 0. Position of MSB in x = 3 Position of MSB in y = 4 Since positions are different, return result. Example 2 : x = 17, y = 19 Result is initially 0. Position of MSB in x = 4 Position of MSB in y = 4 Since positions are same, we compute 24. We add 24 to result. Result becomes 16. We subtract this value from x and y. New value of x = x - 24 = 17 - 16 = 1 New value of y = y - 24 = 19 - 16 = 3 Position of MSB in new x = 1 Position of MSB in new y = 2 Since positions are different, we return result.
More efficient solution
- Flip the LSB of b.
- And check if the new number is in range(a < number < b) or not
- if the number greater than 'a' again flip lsb
- if it is not then that's the answer
NOTE: We are not considering inclusive range so if the number is 'a' we shouldn't flip anymore i.e.'a' will be the answer.
Example 1 : x = 10, y = 20 y = 10100 Flip LSB of y New Number = 10000 i.e. 16 16 is in range so Again flip : 00000 i.e. 0 and 0 is not in range so 0 is the answer. Example 2 : x = 17, y = 19 y = 10011 Flip LSB of y so New Number = 10010 i.e. 18 18 is in range so Again flip : 10000 i.e. 16 and 16 is 'a' so Stop , answer is 16.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Bitwise right shift operators in Java
- Russian Peasant (Multiply two numbers using bitwise operators)
- Bitwise Operators in C/C++
- Check if a number is multiple of 9 using bitwise operators
- Sum of Bitwise And of all pairs in a given array
- Copy set bits in a range
- Find even occurring elements in an array of limited range
- What are the differences between bitwise and logical AND operators in C/C++?
- Numbers whose bitwise OR and sum with N are equal
- Maximum XOR value of a pair from a range
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Toggle case of a string using Bitwise Operators
- Replace every element of the array with BitWise XOR of all other
- Toggle bits in the given range
- Unset bits in the given range