01: /*
02: * $RCSfile: MathJAI.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2005/02/11 04:57:01 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.util;
13:
14: /**
15: * A utility class to contain miscellaneous static methods.
16: */
17: public class MathJAI {
18: /**
19: * Calculate the smallest positive power of 2 greater than or equal to
20: * the provided parameter.
21: *
22: * @param n The value for which the next power of 2 is to be found.
23: * @return The smallest power of 2 >= <i>n</i>.
24: */
25: public static final int nextPositivePowerOf2(int n) {
26: if (n < 2) {
27: return 2;
28: }
29:
30: int power = 1;
31: while (power < n) {
32: power <<= 1;
33: }
34:
35: return power;
36: }
37:
38: /**
39: * Determine whether the parameter is equal to a positive power of 2.
40: *
41: * @param n The value to check.
42: * @return Whether <code>n</code> is a positive power of 2.
43: */
44: public static final boolean isPositivePowerOf2(int n) {
45: return n == nextPositivePowerOf2(n);
46: }
47: }
|