Round up to Power of

Source: Internet
Author: User

This headline should explain what we are going to do, Chinese means finding a 2^n number so that it is not less than the given number. For example: If you give a number 63, then I need to get a number of not less than 63, but this number needs to be 2 of the n-th square, so

    • 63 corresponds to 64 (2^6)
    • 64 corresponds to still 64 (2^6)
    • 100 corresponds to 128 (2^7)

Here's the problem:

How to calculate this result quickly?

Perhaps the first thing that comes to our eyes is the calculation of log or some other non-bitwise operation algorithms, which are not explained again, and look at how the JDK and Android source packages are calculated.

HashMap is a common data result, the bottom is the combination of arrays and linked lists, in order to be able to make the key evenly distributed, reduce collisions, hashmap capacity is 2^n, the capacity is 2^n another benefit is to calculate hashcode% size can be used with the operation instead of ( Implementation away can be viewed on Google), when we need to construct a specified capacity (recorded as Sizea) HashMap, HashMap help us calculate the Sizeb,sizeb to meet 2^n not less than Sizea.
Specifically implemented in Android Java.util.Collections:

 /** * Returns the smallest power of the >= its argument, with several caveats: * If the argument is Negativ e but not Integer.min_value, the method returns * zero. If the argument is > 2^30 or equal to Integer.min_value, the method * returns Integer.min_value.     If The argument is zero, the method returns * zero. * * @hide * *    Public   static int rounduptopoweroftwo(int i) {i--;//If input is a power of both, shift its high-order bit right.        //"Smear" the High-order bit all the the-the-right.I |= i >>>1; I |= i >>>2; I |= i >>>4; I |= i >>>8; I |= i >>> -;returni +1; }

and the implementation in the JDK source code:

     private static int roundUpToPowerOf2(int number) {//Assert number >= 0: "Number must be non-negative";        returnNumber >= maximum_capacity? Maximum_capacity: (Number >1) ? Integer.highestonebit (Number-1) <<1) :1; }Public   static int highestonebit(int i) {//HD, figure 3-1I |= (i >>1); I |= (i >>2); I |= (i >>4); I |= (i >>8); I |= (i >> -);returnI-(i >>>1); }

Do you understand the principle? can go to the little fat Xuan View principle explanation

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Round up to Power of

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.