net.cscott.jutil

Class BitString

public final class BitString extends Object implements Cloneable, Serializable

BitString implements a vector of bits ; much like java.util.BitSet... except that this implementation actually works. Also, BitString has some groovy features which BitSet doesn't; mostly related to efficient iteration over true and false components, I think.

Each component of the BitString has a boolean value. The bits of a BitString are indexed by non-negative integers (that means they are zero-based, of course). Individual indexed bits can be examined, set, or cleared. One BitString may be used to modify the contents of another BitString through logical AND, logical inclusive OR, and logical exclusive OR operations.

By default, all bits in the set initially have the value false.

Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set related to the logical length of a bit set and is defined independently of implementation.

Version: $Id: BitString.java,v 1.1 2003/03/20 01:58:20 cananian Exp $

Author: John Whaley

Constructor Summary
BitString(int nbits)
Creates an empty string with the specified size.
Method Summary
booleanand(BitString set)
Logically ANDs this bit set with the specified set of bits.
voidclear(int bit)
Clears a bit.
voidclearAll()
Clears all bits.
voidclearUpTo(int bit)
Clears all bits up to and including the given bit.
BitStringclone()
Clones the BitString.
voidcopyBits(BitString set)
Copies the values of the bits in the specified set into this set.
booleanequals(Object obj)
Compares this object against the specified object.
intfirstSet()
Returns the first index in the bit string which is set, or -1 if there is no such index.
intfirstSet(int where)
Returns the first index greater than where in the bit string which is set, or -1 if there is no such index.
booleanget(int bit)
Gets a bit.
inthashCode()
Returns a hash code value for this bit string whose value depends only on which bits have been set within this BitString.
booleanintersectionEmpty(BitString other)
Check if the intersection of the two sets is empty
booleanisZero()
intlastSet(int where)
Returns the last index less than where in the bit string which is set, or -1 if there is no such index.
intlastSet()
Returns the last index in the bit string which is set, or -1 if there is no such index.
intlength()
Returns the "logical size" of this BitString: the index of the highest set bit in the BitString plus one.
static voidmain(String[] argv)
Self-test function.
intnumberOfOnes()
intnumberOfOnes(int where)
booleanor(BitString set)
Logically ORs this bit set with the specified set of bits.
booleanor_upTo(BitString set, int bit)
Logically ORs this bit set with the specified set of bits.
voidset(int bit)
Sets a bit.
voidsetAll()
Sets all bits.
voidsetUpTo(int bit)
Sets all bits up to and including the given bit.
intsize()
Returns the number of bits of space actually in use by this BitString to represent bit values.
StringtoString()
Converts the BitString to a String.
booleanxor(BitString set)
Logically XORs this bit set with the specified set of bits.

Constructor Detail

BitString

public BitString(int nbits)
Creates an empty string with the specified size.

Parameters: nbits the size of the string

Method Detail

and

public boolean and(BitString set)
Logically ANDs this bit set with the specified set of bits. Returns true if this was modified in response to the operation.

Parameters: set the bit set to be ANDed with

clear

public void clear(int bit)
Clears a bit.

Parameters: bit the bit to be cleared (zero-based)

clearAll

public void clearAll()
Clears all bits.

clearUpTo

public void clearUpTo(int bit)
Clears all bits up to and including the given bit.

Parameters: bit the bit to be set up to (zero-based)

clone

public BitString clone()
Clones the BitString.

copyBits

public void copyBits(BitString set)
Copies the values of the bits in the specified set into this set.

Parameters: set the bit set to copy the bits from

equals

public boolean equals(Object obj)
Compares this object against the specified object.

Parameters: obj the object to compare with

Returns: true if the contents of the bitsets are the same; false otherwise.

firstSet

public int firstSet()
Returns the first index in the bit string which is set, or -1 if there is no such index.

firstSet

public int firstSet(int where)
Returns the first index greater than where in the bit string which is set, or -1 if there is no such index.

Parameters: where the starting point for the search. May be negative.

get

public boolean get(int bit)
Gets a bit.

Parameters: bit the bit to be gotten (zero-based)

hashCode

public int hashCode()
Returns a hash code value for this bit string whose value depends only on which bits have been set within this BitString.

intersectionEmpty

public boolean intersectionEmpty(BitString other)
Check if the intersection of the two sets is empty

Parameters: set the set to check intersection with

isZero

public boolean isZero()

lastSet

public int lastSet(int where)
Returns the last index less than where in the bit string which is set, or -1 if there is no such index.

Parameters: where the starting point for the search.

lastSet

public int lastSet()
Returns the last index in the bit string which is set, or -1 if there is no such index.

length

public int length()
Returns the "logical size" of this BitString: the index of the highest set bit in the BitString plus one. Returns zero if the BitString contains no set bits.

main

public static void main(String[] argv)
Self-test function.

numberOfOnes

public int numberOfOnes()

numberOfOnes

public int numberOfOnes(int where)

or

public boolean or(BitString set)
Logically ORs this bit set with the specified set of bits. Returns true if this was modified in response to the operation.

Parameters: set the bit set to be ORed with

or_upTo

public boolean or_upTo(BitString set, int bit)
Logically ORs this bit set with the specified set of bits. Returns true if this was modified in response to the operation.

Parameters: set the bit set to be ORed with

set

public void set(int bit)
Sets a bit.

Parameters: bit the bit to be set (zero-based)

setAll

public void setAll()
Sets all bits.

setUpTo

public void setUpTo(int bit)
Sets all bits up to and including the given bit.

Parameters: bit the bit to be set up to (zero-based)

size

public int size()
Returns the number of bits of space actually in use by this BitString to represent bit values. The maximum element in the set is the size - 1st element. The minimum element in the set is the zero'th element.

toString

public String toString()
Converts the BitString to a String.

xor

public boolean xor(BitString set)
Logically XORs this bit set with the specified set of bits. Returns true if this was modified in response to the operation.

Parameters: set the bit set to be XORed with

Copyright © 2003 C. Scott Ananian