28import org.jau.util.BitMath;
29import org.jau.util.Bitfield;
39 private static final int UNIT_SIZE = 32;
60 private static final void check(
final int size,
final int bitnum)
throws IndexOutOfBoundsException {
61 if( 0 > bitnum || bitnum >=
size ) {
62 throw new IndexOutOfBoundsException(
"Bitnum should be within [0.."+(
size-1)+
"], but is "+bitnum);
67 public final int get32(
final int lowBitnum,
final int length)
throws IndexOutOfBoundsException {
68 if( 0 > length || length > 32 ) {
69 throw new IndexOutOfBoundsException(
"length should be within [0..32], but is "+length);
71 check(UNIT_SIZE-length+1, lowBitnum);
72 final int left = 32 - lowBitnum;
79 final int l = Math.min(length, left);
80 final int m = ( 1 << l ) - 1;
81 return m & ( storage >>> lowBitnum );
85 public final void put32(
final int lowBitnum,
final int length,
final int data)
throws IndexOutOfBoundsException {
86 if( 0 > length || length > 32 ) {
87 throw new IndexOutOfBoundsException(
"length should be within [0..32], but is "+length);
89 check(UNIT_SIZE-length+1, lowBitnum);
90 final int left = 32 - lowBitnum;
94 storage = ( ( ~m ) & storage )
98 final int l = Math.min(length, left);
99 final int m = ( 1 << l ) - 1;
100 storage = ( ( ~( m << lowBitnum ) ) & storage )
101 | ( ( m & data ) << lowBitnum );
105 public final int copy32(
final int srcBitnum,
final int dstBitnum,
final int length)
throws IndexOutOfBoundsException {
106 final int data =
get32(srcBitnum, length);
107 put32(dstBitnum, length, data);
112 public final boolean get(
final int bitnum)
throws IndexOutOfBoundsException {
113 check(UNIT_SIZE, bitnum);
114 return 0 != ( storage & ( 1 << bitnum ) ) ;
117 public final boolean put(
final int bitnum,
final boolean bit)
throws IndexOutOfBoundsException {
118 check(UNIT_SIZE, bitnum);
119 final int m = 1 << bitnum;
120 final boolean prev = 0 != ( storage & m ) ;
131 public final void set(
final int bitnum)
throws IndexOutOfBoundsException {
132 check(UNIT_SIZE, bitnum);
133 final int m = 1 << bitnum;
137 public final void clear (
final int bitnum)
throws IndexOutOfBoundsException {
138 check(UNIT_SIZE, bitnum);
139 final int m = 1 << bitnum;
143 public final boolean copy(
final int srcBitnum,
final int dstBitnum)
throws IndexOutOfBoundsException {
144 check(UNIT_SIZE, srcBitnum);
145 check(UNIT_SIZE, dstBitnum);
147 final boolean bit = 0 != ( storage & ( 1 << srcBitnum ) ) ;
149 final int m = 1 << dstBitnum;
Simple bitfield interface for efficient storage access in O(1).
int bitCount()
Returns the number of one bits within this bitfield.
final void put32(final int lowBitnum, final int length, final int data)
Puts length bits of given data into this storage, starting w/ the lowest bit to the storage position ...
final int copy32(final int srcBitnum, final int dstBitnum, final int length)
Copies length bits at position srcLowBitnum to position dstLowBitnum and returning the bits.
final boolean put(final int bitnum, final boolean bit)
Set or clear the bit at position bitnum according to bit and return the previous value.
int size()
Returns the storage size in bit units, e.g.
final int get32(final int lowBitnum, final int length)
Returns length bits from this storage, starting with the lowest bit from the storage position lowBitn...
final void clearField(final boolean bit)
Set all bits of this bitfield to the given value bit.
final void clear(final int bitnum)
Clear the bit at position bitnum according to bit.
final boolean copy(final int srcBitnum, final int dstBitnum)
Copies the bit at position srcBitnum to position dstBitnum and returning true if the bit is set,...
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...
static int getBitMask(final int n)
Returns the 32 bit mask of n-bits, i.e.
static final int UNSIGNED_INT_MAX_VALUE
Maximum 32 bit Unsigned Integer Value: 0xffffffff == {@value}.
Simple bitfield interface for efficient bit storage access in O(1).