public final class BitString extends Object implements Cloneable, Serializable
Implementation of a fixed-length bit-string. Internally, bits are packed into an array of ints. This implementation makes more efficient use of space than the alternative approach of using an array of booleans.
This class is preferable to BitSet
if a fixed number of bits is
required.
Constructor and Description |
---|
BitString(int length)
Creates a bit string of the specified length with all bits
initially set to zero (off).
|
BitString(int length,
Random rng)
Creates a bit string of the specified length with each bit set
randomly (the distribution of bits is uniform so long as the output
from the provided RNG is also uniform).
|
BitString(String value)
Initialises the bit string from a character string of 1s and 0s
in big-endian order.
|
Modifier and Type | Method and Description |
---|---|
BitString |
clone() |
int |
countSetBits() |
int |
countUnsetBits() |
boolean |
equals(Object o) |
void |
flipBit(int index)
Inverts the value of the bit at the specified index.
|
boolean |
getBit(int index)
Returns the bit at the specified index.
|
int |
getLength() |
int |
hashCode()
Over-ridden to be consistent with
equals(Object) . |
void |
setBit(int index,
boolean set)
Sets the bit at the specified index.
|
void |
swapSubstring(BitString other,
int start,
int length)
An efficient method for exchanging data between two bit strings.
|
BigInteger |
toNumber()
Interprets this bit string as being a binary numeric value and returns
the integer that it represents.
|
String |
toString()
Creates a textual representation of this bit string in big-endian
order (index 0 is the right-most bit).
|
public BitString(int length)
length
- The number of bits.public BitString(int length, Random rng)
length
- The number of bits.rng
- A source of randomness.public BitString(String value)
value
- A character string of ones and zeros.public int getLength()
public boolean getBit(int index)
index
- The index of the bit to look-up (0 is the least-significant bit).IndexOutOfBoundsException
- If the specified index is not a bit
position in this bit string.public void setBit(int index, boolean set)
index
- The index of the bit to set (0 is the least-significant bit).set
- A boolean indicating whether the bit should be set or not.IndexOutOfBoundsException
- If the specified index is not a bit
position in this bit string.public void flipBit(int index)
index
- The bit to flip (0 is the least-significant bit).IndexOutOfBoundsException
- If the specified index is not a bit
position in this bit string.public int countSetBits()
public int countUnsetBits()
public BigInteger toNumber()
BigInteger
that contains the numeric value represented
by this bit string.public void swapSubstring(BitString other, int start, int length)
other
- The bitstring with which this bitstring should swap bits.start
- The start position for the substrings to be exchanged. All bit
indices are big-endian, which means position 0 is the rightmost bit.length
- The number of contiguous bits to swap.public String toString()
public BitString clone()
public boolean equals(Object o)
public int hashCode()
equals(Object)
.Copyright © 2015. All rights reserved.