diff options
author | Sven Gothel <[email protected]> | 2014-01-09 20:23:35 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-01-09 20:23:35 +0100 |
commit | 12fe6a6d8fcce42ccfec9e744e3b834aeaf9608b (patch) | |
tree | cd8973ac6d7a28e2e24a9b2242b5f9251e11a73b /src/java/com/jogamp/common/util | |
parent | fd3d0dcf4db9b84ae346653f438c7fe4d2e45fd9 (diff) |
IntBitfield: Fix ctor: min 1 unit; Fix getBitCount(): Use unsigned right-shift '>>>'
Diffstat (limited to 'src/java/com/jogamp/common/util')
-rw-r--r-- | src/java/com/jogamp/common/util/IntBitfield.java | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/java/com/jogamp/common/util/IntBitfield.java b/src/java/com/jogamp/common/util/IntBitfield.java index 07b24dc..2d2f1ef 100644 --- a/src/java/com/jogamp/common/util/IntBitfield.java +++ b/src/java/com/jogamp/common/util/IntBitfield.java @@ -51,7 +51,7 @@ public class IntBitfield { * @param bitCount */ public IntBitfield(long bitCount) { - final int units = (int) ( ( bitCount + 7L ) >> UNIT_SHIFT_L ); + final int units = (int) Math.max(1L, ( bitCount + 7L ) >>> UNIT_SHIFT_L); this.storage = new int[units]; this.bitsCountL = (long)units << UNIT_SHIFT_L ; this.bitsCountI = bitsCountL > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)bitsCountL; @@ -61,7 +61,7 @@ public class IntBitfield { * @param bitCount */ public IntBitfield(int bitCount) { - final int units = ( bitCount + 7 ) >> UNIT_SHIFT_I; + final int units = Math.max(1, ( bitCount + 7 ) >>> UNIT_SHIFT_I); this.storage = new int[units]; this.bitsCountI = units << UNIT_SHIFT_I; this.bitsCountL = bitsCountI; @@ -84,7 +84,7 @@ public class IntBitfield { /** Return <code>true</code> if the bit at position <code>bitnum</code> is set, otherwise <code>false</code>. */ public final boolean get(long bitnum) { check(bitnum); - final int u = (int) ( bitnum >> UNIT_SHIFT_L ); + final int u = (int) ( bitnum >>> UNIT_SHIFT_L ); final int b = (int) ( bitnum - ( (long)u << UNIT_SHIFT_L ) ); return 0 != ( storage[u] & ( 1 << b ) ) ; } @@ -92,7 +92,7 @@ public class IntBitfield { /** Return <code>true</code> if the bit at position <code>bitnum</code> is set, otherwise <code>false</code>. */ public final boolean get(int bitnum) { check(bitnum); - final int u = bitnum >> UNIT_SHIFT_I; + final int u = bitnum >>> UNIT_SHIFT_I; final int b = bitnum - ( u << UNIT_SHIFT_I ); return 0 != ( storage[u] & ( 1 << b ) ) ; } @@ -103,7 +103,7 @@ public class IntBitfield { */ public final boolean put(long bitnum, boolean bit) { check(bitnum); - final int u = (int) ( bitnum >> UNIT_SHIFT_L ); + final int u = (int) ( bitnum >>> UNIT_SHIFT_L ); final int b = (int) ( bitnum - ( (long)u << UNIT_SHIFT_L ) ); final int m = 1 << b; final boolean prev = 0 != ( storage[u] & m ) ; @@ -123,7 +123,7 @@ public class IntBitfield { */ public final boolean put(int bitnum, boolean bit) { check(bitnum); - final int u = bitnum >> UNIT_SHIFT_I; + final int u = bitnum >>> UNIT_SHIFT_I; final int b = bitnum - ( u << UNIT_SHIFT_I ); final int m = 1 << b; final boolean prev = 0 != ( storage[u] & m ) ; @@ -146,14 +146,19 @@ public class IntBitfield { * </pre> */ public static final int getBitCount(final int n) { + // Note: Original used 'unsigned int', + // hence we use the unsigned right-shift '>>>' int c = n; - c -= (n >> 1) & 033333333333; - c -= (n >> 2) & 011111111111; - return ( (c + ( c >> 3 ) ) & 030707070707 ) % 63; + c -= (n >>> 1) & 033333333333; + c -= (n >>> 2) & 011111111111; + return ( (c + ( c >>> 3 ) ) & 030707070707 ) % 63; } /** * Returns the number of set bits within this bitfield. + * <p> + * Utilizes {#link {@link #getBitCount(int)}}. + * </p> */ public long getBitCount() { long c = 0; |