org.klomp.snark
Class BitField

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

public class BitField
extends java.lang.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
 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.
 int size()
          Return the size of the BitField.
 java.lang.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 effect 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:
java.lang.IndexOutOfBoundsException - if bit is smaller then zero bigger then size (inclusive).

get

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

Throws:
java.lang.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 java.lang.String toString()
Overrides:
toString in class java.lang.Object