Here you can find the source of roundUpToTheNextHighestPowerOf2(int v)
Parameter | Description |
---|---|
v | the value for which we need to find the power of 2 such that number is greater than or equal to v. |
public static int roundUpToTheNextHighestPowerOf2(int v)
//package com.java2s; //License from project: Open Source License public class Main { /**/* w ww.ja v a 2s . c o m*/ * Compute the power of 2 which produces an number containing some particular * value. * * @param v the value for which we need to find the power of 2 such that * number is greater than or equal to v. * * @return a number such that 2 to the power of that number contains v */ public static int roundUpToTheNextHighestPowerOf2(int v) { // from: http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } }