Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/util/Pow2.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 /*
15 * Original License: https://github.com/JCTools/JCTools/blob/master/LICENSE
16 * Original location: https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/util/Pow2.java
17 */
18
19 package io.reactivex.rxjava3.internal.util;
20
21 public final class Pow2 {
22 private Pow2() {
23 throw new IllegalStateException("No instances!");
24 }
25
26 /**
27 * Find the next larger positive power of two value up from the given value. If value is a power of two then
28 * this value will be returned.
29 *
30 * @param value from which next positive power of two will be found.
31 * @return the next positive power of 2 or this value if it is a power of 2.
32 */
33 public static int roundToPowerOfTwo(final int value) {
34 return 1 << (32 - Integer.numberOfLeadingZeros(value - 1));
35 }
36
37 /**
38 * Is this value a power of two.
39 *
40 * @param value to be tested to see if it is a power of two.
41 * @return true if the value is a power of 2 otherwise false.
42 */
43 public static boolean isPowerOfTwo(final int value) {
44 return (value & (value - 1)) == 0;
45 }
46 }