jaulib v1.3.0
Jau Support Library (C++, Java, ..)
BitMath.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.util;
25
26public class BitMath {
27 /** Maximum 32 bit Unsigned Integer Value: {@code 0xffffffff} == {@value}. */
28 public static final int UNSIGNED_INT_MAX_VALUE = 0xffffffff;
29
30 /**
31 * Maximum 32bit integer value being of {@link #isPowerOf2(int)}.
32 * <p>
33 * We rely on the JVM spec {@link Integer#SIZE} == 32.
34 * </p>
35 */
36 public static final int MAX_POWER_OF_2 = 1 << ( Integer.SIZE - 2 );
37
38 /**
39 * Returns the 32 bit mask of n-bits, i.e. n low order 1's.
40 * <p>
41 * Implementation handles n == 32.
42 * </p>
43 * @throws IndexOutOfBoundsException if {@code b} is out of bounds, i.e. &gt; 32
44 */
45 public static int getBitMask(final int n) {
46 if( 32 > n ) {
47 return ( 1 << n ) - 1;
48 } else if ( 32 == n ) {
50 } else {
51 throw new IndexOutOfBoundsException("n <= 32 expected, is "+n);
52 }
53 }
54
55 /**
56 * Returns the number of set bits within given 32bit integer in O(1)
57 * using a <i>HAKEM 169 Bit Count</i> inspired implementation:
58 * <pre>
59 * http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html
60 * http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item169
61 * http://tekpool.wordpress.com/category/bit-count/
62 * http://www.hackersdelight.org/
63 * </pre>
64 * <p>
65 * We rely on the JVM spec {@link Integer#SIZE} == 32.
66 * </p>
67 */
68 public static final int bitCount(int n) {
69 // Note: Original used 'unsigned int',
70 // hence we use the unsigned right-shift '>>>'
71 /**
72 * Original does not work due to lack of 'unsigned' right-shift and modulo,
73 * we need 2-complementary solution, i.e. 'signed'.
74 int c = n;
75 c -= (n >>> 1) & 033333333333;
76 c -= (n >>> 2) & 011111111111;
77 return ( (c + ( c >>> 3 ) ) & 030707070707 ) & 0x3f; // % 63
78 */
79 // Hackers Delight, Figure 5-2, pop1 of pop.c.txt
80 n = n - ((n >>> 1) & 0x55555555);
81 n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
82 n = (n + (n >>> 4)) & 0x0f0f0f0f;
83 n = n + (n >>> 8);
84 n = n + (n >>> 16);
85 return n & 0x3f;
86 }
87
88 /**
89 * Returns {@code true} if the given integer is a power of 2
90 * <p>
91 * Source: bithacks: http://www.graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
92 * </p>
93 */
94 public static final boolean isPowerOf2(final int n) {
95 return 0<n && 0 == (n & (n - 1));
96 }
97
98 /**
99 * Returns the next higher power of 2 of 32-bit of given {@code n}
100 * <p>
101 * Source: bithacks: http://www.graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
102 * </p>
103 * <p>
104 * We rely on the JVM spec {@link Integer#SIZE} == 32.
105 * </p>
106 */
107 public static final int nextPowerOf2(int n) {
108 n--;
109 n |= n >>> 1;
110 n |= n >>> 2;
111 n |= n >>> 4;
112 n |= n >>> 8;
113 n |= n >>> 16;
114 return (n < 0) ? 1 : n + 1; // avoid edge case where n is 0, it would return 0, which isn't a power of 2
115 }
116
117 /**
118 * If the given {@code n} is not {@link #isPowerOf2(int)} return {@link #nextPowerOf2(int)},
119 * otherwise return {@code n} unchanged.
120 * <pre>
121 * return isPowerOf2(n) ? n : nextPowerOf2(n);
122 * </pre>
123 * <p>
124 * We rely on the JVM spec {@link Integer#SIZE} == 32.
125 * </p>
126 */
127 public static final int roundToPowerOf2(final int n) {
128 return isPowerOf2(n) ? n : nextPowerOf2(n);
129 }
130
131}
static final int roundToPowerOf2(final int n)
If the given n is not isPowerOf2(int) return nextPowerOf2(int), otherwise return n unchanged.
Definition: BitMath.java:127
static final boolean isPowerOf2(final int n)
Returns true if the given integer is a power of 2.
Definition: BitMath.java:94
static final int bitCount(int n)
Returns the number of set bits within given 32bit integer in O(1) using a HAKEM 169 Bit Count inspire...
Definition: BitMath.java:68
static final int nextPowerOf2(int n)
Returns the next higher power of 2 of 32-bit of given n @endiliteral.
Definition: BitMath.java:107
static int getBitMask(final int n)
Returns the 32 bit mask of n-bits, i.e.
Definition: BitMath.java:45
static final int MAX_POWER_OF_2
Maximum 32bit integer value being of isPowerOf2(int).
Definition: BitMath.java:36
static final int UNSIGNED_INT_MAX_VALUE
Maximum 32 bit Unsigned Integer Value: 0xffffffff == {@value}.
Definition: BitMath.java:28