TITLE: How to find Bitwise AND of all numbers for a given range?
QUESTION [6 upvotes]: How can I find Bitwise AND of all numbers for a given range say from A to B, including both?
I found a beautiful answer for finding XOR for such range. https://stackoverflow.com/a/10670524/2046703How to do this to find AND?

REPLY [4 votes]: For searching leetcode "Bitwise AND of Numbers Range" I reached here, use a long long or python integer doesn't have integer overflowing problem, while if you want to try to use C with regular 32bit signed integer, you've chosen the hard way
I got finally figured out a solution, wonderful for embedded board running environment, this uses the least storage and also runs fast (leetcode says Runtime: 33 ms):
int rangeBitwiseAnd(int m, int n) {
    int x = 0;
    while ((m|x) < n)
       x = (x<<1) | 1;

    return m & ~x;
}