org.klomp.snark
Class BitField

java.lang.Object
  extended by org.klomp.snark.BitField

public class BitField
extends Object

Container of a byte array representing set and unset bits.


Constructor Summary
BitField(byte[] bitfield, int size)
          Creates a new BitField that represents size bits as set by the given byte array.
BitField(int size)
          Creates a new BitField that represents size unset bits.
 
Method Summary
 void clear(int bit)
          Sets the given bit to false.
 boolean complete()
          Return true if all bits are set.
 int count()
          Return the number of set bits.
 boolean get(int bit)
          Return true if the bit is set or false if it is not.
 byte[] getFieldBytes()
          This returns the actual byte array used.
 void set(int bit)
          Sets the given bit to true.
 void setAll()
          Sets all bits to true.
 int size()
          Return the size of the BitField.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitField

public BitField(int size)
Creates a new BitField that represents size unset bits.


BitField

public BitField(byte[] bitfield,
                int size)
Creates a new BitField that represents size bits as set by the given byte array. This will make a copy of the array. Extra bytes will be ignored.

Throws:
ArrayOutOfBoundsException - if give byte array is not large enough.
Method Detail

getFieldBytes

public byte[] getFieldBytes()
This returns the actual byte array used. Changes to this array affect this BitField. Note that some bits at the end of the byte array are supposed to be always unset if they represent bits bigger then the size of the bitfield.


size

public int size()
Return the size of the BitField. The returned value is one bigger then the last valid bit number (since bit numbers are counted from zero).


set

public void set(int bit)
Sets the given bit to true.

Throws:
IndexOutOfBoundsException - if bit is smaller then zero bigger then size (inclusive).

clear

public void clear(int bit)
Sets the given bit to false.

Throws:
IndexOutOfBoundsException - if bit is smaller then zero bigger then size (inclusive).
Since:
0.9.22

setAll

public void setAll()
Sets all bits to true.

Since:
0.9.21

get

public boolean get(int bit)
Return true if the bit is set or false if it is not.

Throws:
IndexOutOfBoundsException - if bit is smaller then zero bigger then size (inclusive).

count

public int count()
Return the number of set bits.


complete

public boolean complete()
Return true if all bits are set.


toString

public String toString()
Overrides:
toString in class Object