Java Integer Clamp clampPower(int num)

Here you can find the source of clampPower(int num)

Description

clamp Power

License

Open Source License

Declaration

public final static int clampPower(int num) 

Method Source Code

//package com.java2s;

public class Main {
    public final static int clampPower(int num) {

        int initialCapacity = 1;
        // Find the best power of two to hold elements.
        // Tests "<=" because arrays aren't kept full.
        if (num >= initialCapacity) {
            initialCapacity = num;//from  w ww. j  av a2  s  . c om
            initialCapacity |= (initialCapacity >>> 1);
            initialCapacity |= (initialCapacity >>> 2);
            initialCapacity |= (initialCapacity >>> 4);
            initialCapacity |= (initialCapacity >>> 8);
            initialCapacity |= (initialCapacity >>> 16);
            initialCapacity++;

            if (initialCapacity < 0) // Too many elements, must back off
                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
        }

        return initialCapacity;
    }
}

Related

  1. clampInt(int value, int min, int max)
  2. CLAMPIS(int a, int b, int c)
  3. clampLoop(int v, int min, int max)
  4. clampMono(int value)
  5. clampNonNegative(int i, int a, int b)
  6. clampRGB(int val)
  7. clampString(String string, int limit)
  8. clampTo_0_255(int i)
  9. clampToByte(int c)