Java examples for java.lang:Math Calculation
Get the smallest power of 2 > x.
/*//from w w w . ja va 2 s. c o m * Copyright (c) 2014. Real Time Genomics Limited. * * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement * for Academic Non-commercial Research Purposes only. * * If you did not receive a license accompanying this file, a copy must first be obtained by email * from support@realtimegenomics.com. On downloading, using and/or continuing to use this source * code you accept the terms of that license agreement and any amendments to those terms that may * be made from time to time by Real Time Genomics Limited. */ //package com.java2s; public class Main { /** * Get the smallest power of 2 > x. Undefined for negative numbers or numbers * so large that there is no positive power of 2 available. * * @param x number being checked. * @return power of 2 > x. */ public static int ceilPowerOf2Bits(final long x) { if (x >= (1L << 62) || x < 0) { throw new IllegalArgumentException("Number out of range:" + x); } long i = 1L; int n = 0; while (i <= x) { assert i > 0; assert n >= 0; n++; i = i << 1; } assert 1L << n == i; return n; } }