201 Bitwise and of Numbers Range

Source: Internet
Author: User

Given a range [M, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, Inclus Ive.

For example, given the range [5, 7], and you should return 4.

int Rangebitwiseand (int m, int n) {    int i, Mbit, match, Val;    Match = val = i = 0;    Mbit =-1; The most significant bit initialization cannot be 0    if ((M < 0) | | (n > 2147483647) | | (M > N))        return 0;    for (i = +; I >= 0; i--)//Gets the most significant bit of n    {        if (N & (0x01 << i))//n in the bit is 1        {            mbit = i;            break;        }    }    if (mbit! =-1)//There is at least one valid bit    {        for (i = Mbit; I >= 0; i--)//starting from the highest bit, the number of equal numbers of M, n consecutive highs        {            if (N & (0 x01 <<i)) = = (M & (0x01<< i))                match++;            else break                ;        }        for (i = 0; i < match; i++)//Get the value of each match bit        {            val = val | (N & (0x01 << mbit--));        }        return val;    }    else        return 0;}

Submission Details
8266/8266 Test cases passed. status:accepted
Runtime: Ms Submitted: 6 minutes ago



Title Introduction: all the numbers between M and N, and get their results . point: the stupidest way is to put all the numbers in the real phase, which is undoubtedly time-consuming and inefficient. It can be found that if the maximum effective bit length of two digits is unequal, the final result must beA value that contains only the most significant bit of n, eg. m = 0x1kkk, n = 0x1kkk kkkk,k for 0 or 1, and final m to n for all numbers and the result is 0x10000 0000; If two numbers are the most effectiveThe bit lengths are equal, then the equal bits are compared from the high to the other until they are unequal, the equal bits are reserved, the unequal position is 0, and the result after, eg. m = 0x1010 0100,n = 0x1010 1111, then the result of all numbers between M and n is 0x1010 0000;
Traps:1. Note that the most significant bit initialization cannot be 0 because >=0 are valid values. 2. Before you can match the bit length, you need to determine if Mbit is-1, otherwise i = Mbit; the statement may cause an error to be executed once in the For loop (excluded in this program). 3. Start matching from the most significant bit, not just for 1,eg. 1010 and 1011, the valid bit is three bits.




201 Bitwise and of Numbers Range

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.