Evaluate the next number of N to the power of 2 m

Source: Internet
Author: User

N is not the power of 2 m, and the next one is required to meet the power of 2 m


This algorithm is available in many places, but it is not explained in detail. It's just a bit of ignorance to make a memo. it's useful to you.

The following is where I ask others. Maybe their explanations are easier for you to understand.

Http://topic.csdn.net/u/20110926/09/8ac6fb0f-d59a-4506-8536-c6d1bbb4ea79.html

public static int test(int n){            n -= 1;            n |= n >>> 1;            System.out.println(n);            n |= n >>> 2;            System.out.println(n);            n |= n >>> 4;            System.out.println(n);            n |= n >>> 8;            System.out.println(n);            n |= n >>> 16;            System.out.println(n);            return n + 1;    }

The reason why n is reduced by one is to satisfy the situation where N is the M power of 2.

The maximum bit 1 is extended to all the following bits, and then add one more

The relationship between the M power of 2 and the M power-1 of 2 is the key of this algorithm. For example, the difference between 100 and 111 is one. From another point of view, we can think that 100 is 111 plus one to get 100.

The next M power of 101 is 100, that is, four. We can do this to extend the maximum bit 1 to all the following bits.

First, shift n to the right logic by one digit (the shifts mentioned later are all logical shifts), and then evaluate or operate with N and save it as n. In this way, the highest and first places on the right are 1,

The next step is to repeat this action, but how can we efficiently overwrite all the bits on the right to make them 1?

The first shift and then seek or, we have obtained the highest bit and the second High is 1, then we move the two places to the right on the basis of the first step, and then find or with the final result obtained for the first time,

Is it because the maximum four digits are 1?

Now we have obtained a maximum of 4 bits, all of which is 1. For the third right shift, we can shift 4 bits at a time. After the shift, we can get a maximum of 8 bits for 1.

For the fourth time, we move eight bits at a time, and then seek or, we can see that the maximum 16 bits are all 1.

For the fifth time, we move another 16 bits. Now all the bits below the highest bits are 1, in the form of 11... 111.

Finally, we add one to this value and obtain the next M power of n that conforms to 2.

Here, I found that I don't have to worry about it. First, I should judge whether this number is a power of 2 m.

N & (n-1) = 0

If it is equal to zero, it indicates that it meets the conditions and returns

If the value is not equal to zero, the number is shifted to one place. If the value is cleared after the highest bit, the result is no longer displayed.

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.