SSF.OS.BGP4.Util
Class BitString

java.lang.Object
  |
  +--SSF.OS.BGP4.Util.BitString

public class BitString
extends java.lang.Object

This class implements a binary string. It is sort of a cross between a boolean array and a java.util.BitSet. It has the fast array access property of a boolean array, but the nice, intuitive interface of BitSet. (Thinking about a boolean array as a sequence of bits can get annoying.) The only bad thing is that an object of this type is not nearly as space-efficient as that of a BitSet.


Constructor Summary
BitString(boolean[] b)
          Constructs a BitString from a boolean array.
BitString(int val, int size)
          Converts an integer into an array of bits.
BitString(long val, int size)
          Converts a long integer into an array of bits.
 
Method Summary
 void and(BitString bs)
          Performs a logical AND between this BitString and the given BitString and assigns the results to this BitString.
static BitString and(BitString bs1, BitString bs2)
          Performs a logical AND between this two given BitStrings and returns the result in a new BitString.
 boolean bgetlr(int bitnum)
          Returns the value of the given bit as a boolean.
 boolean bgetrl(int bitnum)
          Return the value of the given bit as a boolean.
 void clear(int bitnum)
          Sets the value of the given bit to zero.
 int getlr(int bitnum)
          Returns the value of the given bit as an integer.
 int getrl(int bitnum)
          Returns the value of the given bit as an integer.
 void not()
          Performs a logical NOT operation on this BitString and assigns the results to this BitString.
static BitString not(BitString bs)
          Performs a logical NOT on this BitStrings and returns the result in a new BitString.
 void or(BitString bs)
          Performs a logical OR between this BitString and the given BitString and assigns the results to this BitString.
static BitString or(BitString bs1, BitString bs2)
          Performs a logical OR between this two given BitStrings and returns the result in a new BitString.
 BitString revSubstring(int s)
          Returns a substring of the reverse of the bit string given one index and assuming the end of the string as the other index.
 BitString revSubstring(int s, int e)
          Returns a substring of the reverse of the bit string given two indices.
 void set(int bitnum)
          Sets the value of the given bit to one.
 int size()
          Returns the length of the string (the number of bits).
 BitString substring(int s)
          Returns a substring of the bit string given one index and assuming the end of the string as the other index.
 BitString substring(int s, int e)
          Returns a substring of the bit string given two indices.
 java.lang.String toString()
          Prints out the bit string.
 void xor(BitString bs)
          Performs a logical XOR between this BitString and the given BitString and assigns the results to this BitString.
static BitString xor(BitString bs1, BitString bs2)
          Performs a logical XOR between this two given BitStrings and returns the result in a new BitString.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitString

public BitString(int val,
                 int size)
Converts an integer into an array of bits. The bits are represented by booleans (false=0, true=1), and the conversion is done in the following way. The lower order bits of the integer will become the lower numbered elements in the bit array. If the size is specified larger than the actual number of bits in the internal representation of the integer, it will be assumed that these non-existent higher order bits are zeroes.

EXAMPLE: BitString(23,8) is called. Mentally we might picture the integer 23 as the bit string 10111 (with any number of 0's optionally prepended). The size parameter tells us that eight of the bits are significant to us, so for convenience we'll represent 23 as 00010111. A bit array 'b' constructed from this input would be b={1,1,1,0,1,0,0,0}, or b[0]=b[1]=b[2]=b[4]=1 and b[3]=b[5]=b[6]=b[7]=0, where 0=false and 1=true.

The standard printed representation of the array has the most significant bits on the left and the least significant bits on the right. The array representation has each index corresponding to the significance of the bit it represents. In other words, the element 0 corresponds to the least significant bit, element 1 corresponds to the second least significant bit, and so on through element size-1, which represents the most significant bit.

Parameters:
val - The integer value of the bit string.
size - The number of bits in the bit string.
See Also:
BitString(long,int)

BitString

public BitString(long val,
                 int size)
Converts a long integer into an array of bits.

Parameters:
val - The integer value of the bit string.
size - The number of bits in the bit string.
See Also:
BitString(int,int)

BitString

public BitString(boolean[] b)
Constructs a BitString from a boolean array.

Parameters:
b - A boolean array representing a bit string.
Method Detail

set

public void set(int bitnum)
Sets the value of the given bit to one.

Parameters:
bitnum - The index of a bit in the string.

clear

public void clear(int bitnum)
Sets the value of the given bit to zero.

Parameters:
bitnum - The index of a bit in the string.

getlr

public int getlr(int bitnum)
Returns the value of the given bit as an integer. The index is interpreted by counting from left to right, starting at 0.

Parameters:
bitnum - The index of a bit in the string.
Returns:
the value of the given bit as an integer

bgetlr

public boolean bgetlr(int bitnum)
Returns the value of the given bit as a boolean. The index is interpreted by counting from left to right, starting at 0.

Parameters:
bitnum - The index of a bit in the string.
Returns:
the value of the given bit as a boolean

getrl

public int getrl(int bitnum)
Returns the value of the given bit as an integer. The index is interpreted by counting from right to left, starting at 0.

Parameters:
bitnum - The index of a bit in the string.
Returns:
the value of the given bit as an integer

bgetrl

public boolean bgetrl(int bitnum)
Return the value of the given bit as a boolean. The index is interpreted by counting from right to left, starting at 0.

Parameters:
bitnum - The index of a bit in the string.
Returns:
the value of the given bit as a boolean

size

public int size()
Returns the length of the string (the number of bits).

Returns:
the number of bits in the bit string

and

public void and(BitString bs)
Performs a logical AND between this BitString and the given BitString and assigns the results to this BitString.

Parameters:
bs - The bit string to AND with.

or

public void or(BitString bs)
Performs a logical OR between this BitString and the given BitString and assigns the results to this BitString.

Parameters:
bs - The bit string to OR with.

xor

public void xor(BitString bs)
Performs a logical XOR between this BitString and the given BitString and assigns the results to this BitString.

Parameters:
bs - The bit string to XOR with.

not

public void not()
Performs a logical NOT operation on this BitString and assigns the results to this BitString.


and

public static BitString and(BitString bs1,
                            BitString bs2)
Performs a logical AND between this two given BitStrings and returns the result in a new BitString.

Parameters:
bs1 - The first of two bit strings perform an AND upon.
bs2 - The second of two bit strings perform an AND upon.
Returns:
the result of an AND between two bit strings

or

public static BitString or(BitString bs1,
                           BitString bs2)
Performs a logical OR between this two given BitStrings and returns the result in a new BitString.

Parameters:
bs1 - The first of two bit strings perform an OR upon.
bs2 - The second of two bit strings perform an OR upon.
Returns:
the result of an OR between two bit strings

not

public static BitString not(BitString bs)
Performs a logical NOT on this BitStrings and returns the result in a new BitString.

Parameters:
bs - A bit string to perform a NOT upon.
Returns:
the result of a NOT applied to a bit string

xor

public static BitString xor(BitString bs1,
                            BitString bs2)
Performs a logical XOR between this two given BitStrings and returns the result in a new BitString.

Parameters:
bs1 - The first of two bit strings perform an XOR upon.
bs2 - The second of two bit strings perform an XOR upon.
Returns:
the result of an XOR between two bit strings

substring

public BitString substring(int s,
                           int e)
Returns a substring of the bit string given two indices.

Parameters:
s - The starting index for the substring.
e - The ending index for the substring.
Returns:
a substring of the bit string

substring

public BitString substring(int s)
Returns a substring of the bit string given one index and assuming the end of the string as the other index.

Parameters:
s - The starting index for the substring.
Returns:
a substring of the bit string from the given index to the end of the string

revSubstring

public BitString revSubstring(int s,
                              int e)
Returns a substring of the reverse of the bit string given two indices. In other words, the bit string is reversed, and then the normal substring procedure is applied to the new, reversed string.

Parameters:
s - The starting index for the substring.
e - The ending index for the substring.
Returns:
a substring of the bit string's reverse

revSubstring

public BitString revSubstring(int s)
Returns a substring of the reverse of the bit string given one index and assuming the end of the string as the other index. In other words, the bit string is reversed, and then the normal substring procedure is applied to the new, reversed string.

Parameters:
s - The starting index for the substring.
Returns:
a substring of the bit string's reverse

toString

public java.lang.String toString()
Prints out the bit string. It does so from left to right, from most significant bit to least significant bit.

Overrides:
toString in class java.lang.Object
Returns:
a (text) string representation of the bit string