eaLib API Docs
Last Modified : January 7 2002

mss.ea.core
Class BitVector

java.lang.Object
  |
  +--mss.ea.core.BitVector
All Implemented Interfaces:
java.lang.Cloneable

public class BitVector
extends java.lang.Object
implements java.lang.Cloneable

Replacement for Sun's unflexible BitSet. This class provides an internal boundchecking. If an invalid index is given the appropriate method does simply nothing. For convinience reasons no exception is thrown in such a case.

Since:
0.1
Version:
0.1 (11-30-2000)
Author:
Andreas Rummler

Field Summary
protected static byte[] oneMask
          Byte array, for masking single bits with one.
protected  byte[] set
          All bits are kept in this byte array.
protected  int setSize
          Size of byte array.
protected static byte[] zeroMask
          Byte array, for masking single bits with zero.
 
Constructor Summary
BitVector()
          Constructor with initial size of 8 bits.
BitVector(byte ini)
          Constructs a new bitvector from a byte.
BitVector(byte[] ba)
          Constructor with byte array.
BitVector(byte[] ba, boolean reverse)
          Constructor with byte array.
BitVector(int size)
          Constructor with size of BitVector.
BitVector(int ini, boolean intFlag)
          Constructs a new bitvector from an int number.
BitVector(long ini)
          Constructs a new bitvector from a long number.
BitVector(short ini)
          Constructs a new bitvector from a short number.
BitVector(java.lang.String bitstring)
          Constructor with string.
 
Method Summary
 BitVector and(BitVector bv)
          Performs an AND operation.
 int byteSize()
          This returns the number of the the bytes used for holding the bit vector.
 void clear(int index)
          Clears a single bits.
 java.lang.Object clone()
          Clones the bit vector.
 boolean get(int index)
          Gets a single bit.
 BitVector getBitVector(int index, int length)
          Gets a new bitvector with bits starting at a particular position.
 byte getByte(int byteIndex)
          Gets a byte from the bitvector.
 byte getByte(int bitIndex, int length)
          Gets a byte from the bitvector starting at a particular position.
 int getInteger(int index)
          Gets a single bit and returns the value as an integer.
protected  boolean internalGet(int index)
          Gets a single bit.
protected  void internalSet(int index)
          Sets a single bit.
protected  void internalSet(int index, boolean value)
          Sets a single bit.
protected  void internalUnset(int index)
          Clears a single bit.
 int intValue()
          Returns the lowest 4 bytes from the bit vector as an integer number.
 long longValue()
          Returns the lowest 8 bytes of the bit vector as a long number.
 void not()
          Inverts the bitvector.
 BitVector or(BitVector bv)
          Performs an OR operation.
 void randomize()
          Fills the bit vector with random bits.
 void rclear(int index)
          Clears a single bit and resizes the bit vector, if necessary.
 void rset(int index)
          Sets a single bit and resizes the bit vector, if necessary.
 void runset(int index)
          Clears a single bit and resizes the bit vector, if necessary.
 void set(int index)
          Sets a single bit.
 void set(int index, boolean value)
          Sets a single bit.
 void set(int index, int value)
          Sets a single bit.
 void shiftLeft()
          Shifts the bitvector to the left.
 void shiftLeft(int times)
          Shifts the bitvector n times to the left.
 void shiftRight()
          Shifts the bitvector to the right.
 void shiftRight(int times)
          Shifts the bitvector n times to the right.
 int size()
          Gets the size of the bitvector.
 java.lang.String toBinaryString()
          Returns a string representation of the bitvector.
 java.lang.String toBitSetString()
          Returns a string representation, which equals the one Sun's BitSet returns.
 void toggle(int index)
          Inverts a bit.
static boolean toggleBit(boolean bit)
          Inverts a boolean value.
 java.lang.String toHexString()
          Returns a hexadecimal string representation of the bitvector.
 java.lang.String toHexString(boolean withPrefix)
          Returns a hexadecimal string representation of the bitvector.
 java.lang.String toString()
          Returns a string representation of the bitvector.
 void unset(int index)
          Clears a single bits.
 BitVector xor(BitVector bv)
          Performs an XOR operation.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

set

protected byte[] set
All bits are kept in this byte array.

setSize

protected int setSize
Size of byte array. This value must not be dividable by 8.

zeroMask

protected static final byte[] zeroMask
Byte array, for masking single bits with zero. This array has the size of 8 and contains the masks for bits 0, 1 etc.

oneMask

protected static final byte[] oneMask
Byte array, for masking single bits with one. This array has the size of 8 and contains the masks for bits 0, 1 etc.
Constructor Detail

BitVector

public BitVector()
Constructor with initial size of 8 bits.

BitVector

public BitVector(int size)
Constructor with size of BitVector.
Parameters:
size - size of bit vector

BitVector

public BitVector(byte ini)
Constructs a new bitvector from a byte.
Parameters:
ini - byte to construct bitvector from

BitVector

public BitVector(short ini)
Constructs a new bitvector from a short number.
Parameters:
ini - short number to construct bitvector from

BitVector

public BitVector(int ini,
                 boolean intFlag)
Constructs a new bitvector from an int number.
Parameters:
ini - int number to construct bitvector from
intFlag - this is used to differentiate this constructor from

BitVector

public BitVector(long ini)
Constructs a new bitvector from a long number.
Parameters:
ini - long number to construct bitvector from

BitVector

public BitVector(byte[] ba)
Constructor with byte array. This creates a new bitvector from the given byte array.
Parameters:
ba - initial byte array

BitVector

public BitVector(byte[] ba,
                 boolean reverse)
Constructor with byte array. This creates a new bitvector from the given byte array. The byte vector can be optionally reversed
Parameters:
ba - initial byte array
reverse - if true, reverses the byte vector

BitVector

public BitVector(java.lang.String bitstring)
Constructor with string. This creates a new bitvector from the given string. The string is interpreted in that way that the MSB is the leftmost character. Any characters that are not "0" or "1" are treated as zeros.
Parameters:
bitstring - string that this bit vector is constructed from
Method Detail

get

public boolean get(int index)
Gets a single bit.
Parameters:
index - position of bit in bit vector
Returns:
value of the bit

getInteger

public int getInteger(int index)
Gets a single bit and returns the value as an integer.
Parameters:
index - position of the bit in bit vector
Returns:
value of the bit

getByte

public byte getByte(int byteIndex)
Gets a byte from the bitvector.
Parameters:
byteIndex - position of byte in byte array
Returns:
byte from byte array

getByte

public byte getByte(int bitIndex,
                    int length)
Gets a byte from the bitvector starting at a particular position.
Parameters:
bitIndex - position of the starting bit in the bitvector
length - number of bits to get (values > 8 will be ignored)
Returns:
new byte created from selected bits

getBitVector

public BitVector getBitVector(int index,
                              int length)
Gets a new bitvector with bits starting at a particular position.
Parameters:
index - position of the starting bit in the current bitvector
length - number of bits to get
Returns:
new bitvector created from selected bits

internalGet

protected boolean internalGet(int index)
Gets a single bit. This method is for internal use, it provides the same functionality as get(int), but works without boundchecking.
Parameters:
index - position of the bit in bit vector
Returns:
value of the bit

internalSet

protected void internalSet(int index)
Sets a single bit. This method is for internal use, it provides the same functionality as set(int), but works without boundchecking.
Parameters:
index - position of the bit in bit vector

internalSet

protected void internalSet(int index,
                           boolean value)
Sets a single bit. This method is for internal use, it provides the same functionality as set(int, boolean), but works without boundchecking.
Parameters:
index - position of the bit in bit vector
value - value of the bit

internalUnset

protected void internalUnset(int index)
Clears a single bit. This method is for internal use, it provides the same functionality as unset(int), but works without boundchecking.
Parameters:
index - position of the bit in bit vector

set

public void set(int index)
Sets a single bit.
Parameters:
index - position of the bit in bit vector

set

public void set(int index,
                boolean value)
Sets a single bit.
Parameters:
index - position of the bit in bit vector
value - value of the bit

set

public void set(int index,
                int value)
Sets a single bit.
Parameters:
index - position of the bit in bit vector
value - value of the bit

rset

public void rset(int index)
Sets a single bit and resizes the bit vector, if necessary. Warning: Resizing is done in 8 bit steps. This may be changed in the future.
Parameters:
index - position of the bit in bit vector

unset

public void unset(int index)
Clears a single bits.
Parameters:
index - position of the bit in bit vector

clear

public void clear(int index)
Clears a single bits.
Parameters:
index - position of the bit in bit vector

runset

public void runset(int index)
Clears a single bit and resizes the bit vector, if necessary. Warning: resizing is done in 8 bit steps. This may be changed in the future.
Parameters:
index - position of the bit in bit vector

rclear

public void rclear(int index)
Clears a single bit and resizes the bit vector, if necessary. Warning : resizing is done in 8 bit steps. This may be changed in the future.
Parameters:
index - index of the bit to be cleared

and

public BitVector and(BitVector bv)
Performs an AND operation.
Parameters:
bv - second bitvector to use for for AND operation
Returns:
new bitvector containing result

not

public void not()
Inverts the bitvector.

or

public BitVector or(BitVector bv)
Performs an OR operation.
Parameters:
bv - second bitvector to use for for OR operation
Returns:
new bitvector containing result

xor

public BitVector xor(BitVector bv)
Performs an XOR operation.
Parameters:
bv - second bitvector to use for for XOR operation
Returns:
new bitvector containing result

shiftLeft

public void shiftLeft()
Shifts the bitvector to the left.

shiftLeft

public void shiftLeft(int times)
Shifts the bitvector n times to the left.
Parameters:
times - number of shift operations

shiftRight

public void shiftRight()
Shifts the bitvector to the right.

shiftRight

public void shiftRight(int times)
Shifts the bitvector n times to the right.
Parameters:
times - number of shift operations

size

public int size()
Gets the size of the bitvector.
Returns:
size of the bitvector

byteSize

public int byteSize()
This returns the number of the the bytes used for holding the bit vector.
Returns:
number of bytes

toString

public java.lang.String toString()
Returns a string representation of the bitvector. The output is done in 8 bit blocks. After each block a whitespace is inserted. The most significant block is printed first. In each block the bits are printed from MSB .. LSB.
Overrides:
toString in class java.lang.Object
Returns:
string representation

toBinaryString

public java.lang.String toBinaryString()
Returns a string representation of the bitvector. This method works like toString(), but whitespaces ar missing.
Returns:
string representation

toHexString

public java.lang.String toHexString()
Returns a hexadecimal string representation of the bitvector.
Returns:
hexadecimal string representation

toHexString

public java.lang.String toHexString(boolean withPrefix)
Returns a hexadecimal string representation of the bitvector.
Parameters:
withPrefix - if true, the prefix "0x" is used
Returns:
hexadecimal string representation

toBitSetString

public java.lang.String toBitSetString()
Returns a string representation, which equals the one Sun's BitSet returns. The method is done for convinience reasons.
Returns:
bit set string representation

intValue

public int intValue()
Returns the lowest 4 bytes from the bit vector as an integer number.
Returns:
integer number

longValue

public long longValue()
Returns the lowest 8 bytes of the bit vector as a long number.
Returns:
long number

toggle

public void toggle(int index)
Inverts a bit.
Parameters:
index - position of the bit in bit vector

toggleBit

public static boolean toggleBit(boolean bit)
Inverts a boolean value. This is a static helper method.
Parameters:
bit - value to be inverted
Returns:
inverted value

randomize

public void randomize()
Fills the bit vector with random bits.

clone

public java.lang.Object clone()
Clones the bit vector.
Overrides:
clone in class java.lang.Object
Returns:
copy of the bit vector

eaLib API Docs
Last Modified : January 7 2002

Copyright © 2000-2001 Technical University of Ilmenau, Department ESS