com.sun.labs.minion.util
Class BitBuffer

java.lang.Object
  extended by com.sun.labs.minion.util.BitBuffer
All Implemented Interfaces:
IntEncoder, java.lang.Cloneable

public class BitBuffer
extends java.lang.Object
implements java.lang.Cloneable, IntEncoder

The BitBuffer class is used to represent a string of bits. We're not using a java.util.BitSet because it doesn't support operations, like append, which we would like to have and it doesn't think that zeros at the end of the buffer are significant (they are for us.) BitSet has a solid basis implementation, however, so we'll use it (we can't extend the class because too many variables we need are private.)

The basic idea: An array of ints (called bits) is maintained to store the bits. Each int in this array is called a unit. We store bits starting at unit 0, bit 0, which is the least-significant bit of the int stored in bits[0]. Bits are stored from the least-significant position to the most-significant position in each unit runnning from 0 to the size of the bits array. An example:

              Direction Units filled
 -------------------------------------------->
 ----------------------+----------------------+
         Unit 0        |         Unit 1       |
 3                    0|3                    0|
 2         ...        0|2          ...       0|
 ----------------------+----------------------+
 <---------------------|<---------------------|
 Direction bits filled |Direction bits filled |
 

A "bit index" is simply a number giving a specific bit position in a BitBuffer, so, for example, a bit index of 69 indicates the 5th least-siginificant bit in bits[2] (i.e, bit 5 in bits[2]).

A "bit position" is a position within a unit, i.e., a number between 0 and 31. So, the bit index 69 has bit position 5.

There are three numbers that describe the size of the array and where bits are to be written to or read from: currUnit, which points to the unit where the next bit will be written, currBit, which points to the next bit to be set in the current unit, and head, which contains the bit index of the next bit to be read from the head of the array. All three numbers are indexed from 0.

In general, we expect that bits will be read from the head of the BitBuffer and written to the end of the BitBuffer. BitSet provides methods for setting and clearing a specific bit, which we will use with some modification.

We also provide methods for writing and reading BitBuffers. BitBuffers can be written to any output stream that implements the DataOutput interface. A buffer is written in the following manner:

Buffers can be read from any stream that implements the DataInput interface.

The methods in the class have been synchronized where appropriate to prevent overlapping writes, but you probably don't want to use the same instance in different threads without some other synchronization method. But hey, it's your funeral.


Field Summary
protected static int ADDRESS_BITS_PER_UNIT
           
protected static int BIT_INDEX_MASK
           
protected  int[] bits
          The bits in this BitSet.
protected static int BITS_PER_UNIT
           
protected static int[] ca
          Some zeros we can use to clear out buffers.
protected  int currBit
          The current bit in the last unit.
protected  int currUnit
          The unit where we will write the next bit.
protected static int FULL_UNIT
           
protected  int head
          A pointer to the head of the current Buffer.
protected  int markPoint
          A mark that we can reset to.
protected static int[] maxIntPerBits
          The maximum number that can be encoded in a given number of bits.
 
Constructor Summary
BitBuffer()
          Creates a new bit buffer.
BitBuffer(BitBuffer b)
          Creates a bit buffer whose data is initialized by the bit buffer given as a parameter.
BitBuffer(boolean resetp, BitBuffer b)
          Creates a bit buffer whose data shares the data of the bit buffer given as a parameter.
BitBuffer(byte[] bytes, int offset)
          Creates a bit buffer from an array of bytes.
BitBuffer(java.io.DataInput di)
          Creates a bit buffer by reading it from the provided stream.
BitBuffer(int nbits)
          Creates a bit buffer whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1.
BitBuffer(java.lang.String b)
           
 
Method Summary
 void and(BitBuffer r)
          Calculate the bitwise and of this buffer and another.
 void append(BitBuffer b)
          Append another BitBuffer to this one.
protected static int bit(int bitIndex)
          Given a bit index, return a unit that masks that bit in its unit.
protected static int bitPos(int bitIndex)
          Find the position in a unit of a given bit index.
 int byteDecode()
          Decodes an integer stored using the byte encoding.
 int byteEncode(int n)
          Encodes an integer in a byte-aligned fashion, using the minimal number of bytes.
 int chunkDecode()
          A chunk decoder.
 int chunkEncode(int n)
          A "chunk" encoder, after an idea by Doug Cutting.
 void clear()
          Clears out a bit buffer entirely.
 java.lang.Object clone()
          Cloning this BitBuffer produces a new BitBuffer that is equal to it.
 void complement()
          Calculate the bitwise complement of this buffer.
 void copyNBitsFrom(BitBuffer b, int n)
          Copies n bits from the given buffer onto our buffer.
 int countBits(boolean b)
          Counts the number of bits of the given type in the buffer.
protected  int countTrue()
          Counts the number of on bits in the buffer.
static void createBuffers(BitBuffer[] buffers, byte[] bytes, int[] offsets)
          Creates an array of BitBuffers from the provided array of bytes.
 char[] decodeChars()
          Decodes an array of characters from the head of the buffer.
 char[] decodeChars(int utflen)
           
 void decodeChars(int utflen, CACont str)
           
 int decodeInt()
          Decodes any Java int.
 long decodeLong()
          Decodes any Java long.
 java.lang.String decodeUTF()
          Decodes a string from the head of the buffer.
 int deltaDecode()
          Delta decode an int from the front of the buffer.
 int deltaEncode(int n)
          Delta encode an int onto the end of the buffer.
 int[] differenceDecodeArray()
          Decodes an array of integers using the standard difference method.
 int differenceEncodeArray(int n, int[] a)
          Encodes an array integers using a standard difference method.
 int directDecode(int nBits)
          Decode a positive integer that was coded using a specific number of bits.
 long directDecodeLong(int nBits)
          Direct decodes a long.
 int directEncode(int n, int nBits)
          Encode a positive integer directly, using a given number of bits.
 int directEncode(long n, int nBits)
          Encodes a long directly in the given number of bits.
 int encodeChars(char[] a)
          Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner.
 int encodeChars(char[] a, int b, int e)
          Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner.
static int encodeChars(char[] a, int b, int e, BitBuffer buff)
          Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner.
 int encodeInt(int n)
          Encodes any Java int.
 int encodeLong(long n)
          Encodes any Java long.
 int encodeUTF(java.lang.String s)
          Encodes a string using a modified UTF-8 encoding in a machine independant manner.
 int encodeUTFLen(int n)
          Encodes a number of bytes in a manner suitable for use by decodeChars.
protected  void ensureCapacity(int unitsRequired)
           
 int findFirst(boolean b)
          Find the first instance of a bit matching the bit passed in.
static int findFirstInUnit(boolean b, int unit, int start)
          Find the positon of the first bit matching b in the unit held in unit, starting from position start.
static int flLog2(int n)
          Calculate the floor of the base 2 log of an integer n.
static int flLog2(long n)
          Calculates the floor of the base 2 log for a long.
 int gammaDecode()
          Gamma decode the head of the buffer.
 int gammaEncode(int n)
          Gamma encode an int onto the end of the buffer.
 int getCurrBitIndex()
          Get the index of currBit.
 int getNBytes()
          Get the number of bytes that it will take to store this BitBuffer in a file.
 int getWBytes()
          Gets the total number of bytes that it will take to write this buffer into a file.
protected static java.lang.String intToBinaryString(int n)
          Build a string representation of an int with the bits in the right order.
 int length()
          Get the length of the bit string.
protected static java.lang.String longToBinaryString(long n)
           
 void or(BitBuffer r)
          Calculate the bitwise or of this buffer and another.
 boolean peek()
           
 boolean pop()
          Pop a bit from the head of the current buffer.
 void push(boolean newbit)
          Push a bit onto the end of the buffer.
 void push(boolean newbit, int n)
          Push a bit onto the end of the buffer n times.
 void quickClear()
          Does a quick clear out.
 void read(java.io.DataInput din)
          Read a BitBuffer from an object that implements DataInput.
 void reset()
          Reset the head to point at the start of bits array.
 void seek(int i)
          Seeks to the given bit index.
 void set(int bitIndex)
          Sets the bit specified by the index to true.
static BitBuffer share(BitBuffer shared)
          Share a BitBuffer among multiple readers.
static void skip(java.io.DataInput in)
          Skips a bit buffer without reading it in.
 void skip(int n)
          Skip the given number of bits.
 int tell()
          Tells where the read head currently is.
 boolean test(int bitIndex)
          Test whether a given bit is true or false.
 java.lang.String toActualString()
          Print the bits in the buffer in the order in which they actually occur.
 java.lang.String toLogicalString()
          Provide a logical string representation of the bit buffer.
 java.lang.String toString()
           
 int unaryDecode()
          Unary decode an int from the head of the BitBuffer.
 int unaryEncode(int n)
          Unary encode an integer onto the end of the buffer.
protected static int unitIndex(int bitIndex)
          Given a bit index return unit index containing it.
protected  java.lang.String unitToLogicalString(int unit, int pos)
          Get the bits in a unit from right to left, put them in a string left to right, starting at the given position.
 void write(BitBuffer b)
          Write this BitBuffer to another.
 void write(java.io.DataOutput dout)
          Write a buffer to a stream with the length.
 void write(java.io.DataOutput dout, boolean writeLength)
          Write a BitBuffer to a class implementing DataOuput.
 void xor(BitBuffer r)
          Calculate the bitwise xor of this buffer and another.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ADDRESS_BITS_PER_UNIT

protected static final int ADDRESS_BITS_PER_UNIT
See Also:
Constant Field Values

BITS_PER_UNIT

protected static final int BITS_PER_UNIT
See Also:
Constant Field Values

BIT_INDEX_MASK

protected static final int BIT_INDEX_MASK
See Also:
Constant Field Values

FULL_UNIT

protected static final int FULL_UNIT
See Also:
Constant Field Values

bits

protected int[] bits
The bits in this BitSet. The ith bit is stored in bits[i/64] at bit position i % 32 (where bit position 0 refers to the least significant bit and 31 refers to the most significant bit).


currUnit

protected int currUnit
The unit where we will write the next bit.


currBit

protected int currBit
The current bit in the last unit. This is the bit that will be written next.


head

protected int head
A pointer to the head of the current Buffer.


markPoint

protected int markPoint
A mark that we can reset to.


ca

protected static int[] ca
Some zeros we can use to clear out buffers.


maxIntPerBits

protected static int[] maxIntPerBits
The maximum number that can be encoded in a given number of bits. Used to check directEncode input.

Constructor Detail

BitBuffer

public BitBuffer()
Creates a new bit buffer. All bits are initially false.


BitBuffer

public BitBuffer(int nbits)
Creates a bit buffer whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. All bits are initially false.

Parameters:
nbits - the initial size of the bit set.
Throws:
java.lang.NegativeArraySizeException - if the specified initial size is negative.

BitBuffer

public BitBuffer(BitBuffer b)
Creates a bit buffer whose data is initialized by the bit buffer given as a parameter.

Parameters:
b - The bit buffer to use.

BitBuffer

public BitBuffer(boolean resetp,
                 BitBuffer b)
Creates a bit buffer whose data shares the data of the bit buffer given as a parameter. Useful for shared readers. (pmartin 8 jan 03)

Parameters:
resetp - true resets the read pointer in the new BB
b - The bit buffer to share the data from..

BitBuffer

public BitBuffer(java.io.DataInput di)
          throws java.io.IOException
Creates a bit buffer by reading it from the provided stream.

Parameters:
di - The stream to read from.
Throws:
java.io.IOException - if there is an error during reading.

BitBuffer

public BitBuffer(byte[] bytes,
                 int offset)
Creates a bit buffer from an array of bytes. This array of bytes must have the same structure as the array that is constructed in the write method. The first four bytes of the array must consist of the length of the buffer in bits. The remaining bytes encode the integers of the actual data.

Parameters:
bytes - The array of bytes to convert to a BitBuffer
offset - The offset in the array to start at.

BitBuffer

public BitBuffer(java.lang.String b)
Method Detail

unitIndex

protected static final int unitIndex(int bitIndex)
Given a bit index return unit index containing it.


bit

protected static final int bit(int bitIndex)
Given a bit index, return a unit that masks that bit in its unit.


bitPos

protected static final int bitPos(int bitIndex)
Find the position in a unit of a given bit index.

Returns:
the position in a unit of the given bit index.

getCurrBitIndex

public final int getCurrBitIndex()
Get the index of currBit.


getNBytes

public int getNBytes()
Get the number of bytes that it will take to store this BitBuffer in a file.

Specified by:
getNBytes in interface IntEncoder

getWBytes

public int getWBytes()
Gets the total number of bytes that it will take to write this buffer into a file. This number includes the four bytes that the length of the buffer will take.

Returns:
the number of bytes that this bit buffer will occupy when written to a file.

createBuffers

public static void createBuffers(BitBuffer[] buffers,
                                 byte[] bytes,
                                 int[] offsets)
Creates an array of BitBuffers from the provided array of bytes. The idea is that if we've read multiple buffers from a file, this method can convert them into a list of buffers useful for things like iterating through postings files.


clone

public java.lang.Object clone()
Cloning this BitBuffer produces a new BitBuffer that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set and the same current size.

Overrides the clone method of Object.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this bit buffer.

share

public static BitBuffer share(BitBuffer shared)
Share a BitBuffer among multiple readers. There is a strong assumption for this case that there will be no writers! This will be most useful for multiple iterators through a compressed inverted file entry, where we don't wish to duplicate the array.

Parameters:
shared - The BitBuffer that we will share.
Returns:
A BitBuffer sharing that data.

ensureCapacity

protected void ensureCapacity(int unitsRequired)

length

public int length()
Get the length of the bit string.

Returns:
the length of the bit string.

set

public void set(int bitIndex)
Sets the bit specified by the index to true. If the bit that we set is further out than the current bit index, then reset the position to just after the bit set.

Parameters:
bitIndex - the bit index to set.

test

public boolean test(int bitIndex)
Test whether a given bit is true or false.

Parameters:
bitIndex - the index of the bit to test.
Returns:
true if the bit is 1, false if it is 0

push

public void push(boolean newbit)
Push a bit onto the end of the buffer.


push

public void push(boolean newbit,
                 int n)
Push a bit onto the end of the buffer n times.

Parameters:
newbit - The bit to push.
n - The number of times to push it.

pop

public boolean pop()
Pop a bit from the head of the current buffer. Modifies the buffer by removing the bit.

Returns:
true if the bit at the head of the buffer is 1.

peek

public boolean peek()

countBits

public int countBits(boolean b)
Counts the number of bits of the given type in the buffer.

Parameters:
b - Whether we want to count true or false.

countTrue

protected int countTrue()
Counts the number of on bits in the buffer.


clear

public final void clear()
Clears out a bit buffer entirely.


quickClear

public void quickClear()
Does a quick clear out.


skip

public void skip(int n)
Skip the given number of bits.

Parameters:
n - The number of bits to skip.

seek

public void seek(int i)
Seeks to the given bit index. Subsequent decodes will occur from this point.

Parameters:
i - The index to set the head to.

tell

public int tell()
Tells where the read head currently is.

Returns:
the bit index for the current read position.

findFirstInUnit

public static final int findFirstInUnit(boolean b,
                                        int unit,
                                        int start)
Find the positon of the first bit matching b in the unit held in unit, starting from position start.

Returns:
the position of the bit, or -1 if there is no such bit.

findFirst

public final int findFirst(boolean b)
Find the first instance of a bit matching the bit passed in.

Returns:
the number of bits from the head of the buffer to that bit, or -1 if there is no such bit.

flLog2

public static final int flLog2(int n)
Calculate the floor of the base 2 log of an integer n. We're using this rather than the library routines because it's 3-4 times faster. The floor of the log of an integer is simply the index of the left-most 1 bit in the number.

This is implemented as an unrolled binary search and is therefore totally out of control (but hopefully fast.)

Parameters:
n - The int to calculate flLog2 for
Returns:
The floor of the log to the base 2 of the number.

flLog2

public static final int flLog2(long n)
Calculates the floor of the base 2 log for a long.


reset

public void reset()
Reset the head to point at the start of bits array.


directEncode

public final int directEncode(int n,
                              int nBits)
Encode a positive integer directly, using a given number of bits.

Parameters:
n - The number to encode.
nBits - The number of bits to use in the encoding.
Returns:
The number of bits used in the encoding.

directEncode

public final int directEncode(long n,
                              int nBits)
Encodes a long directly in the given number of bits.


directDecodeLong

public final long directDecodeLong(int nBits)
Direct decodes a long.


directDecode

public final int directDecode(int nBits)
Decode a positive integer that was coded using a specific number of bits.

Parameters:
nBits - The number of bits to use.
Returns:
the decoded integer.

unaryEncode

public final int unaryEncode(int n)
Unary encode an integer onto the end of the buffer. The unary encoding of an integer n is n-1 1 bits followed by a 0 bit.

Parameters:
n - The number to encode.
Returns:
The number of bits used in the encoding.

unaryDecode

public final int unaryDecode()
Unary decode an int from the head of the BitBuffer.

Returns:
the decoded number.

chunkEncode

public final int chunkEncode(int n)
A "chunk" encoder, after an idea by Doug Cutting. Sort of a "chunkier" gamma encoding. We unary encode the number of bytes that the number can fit into, and then direct encode the number in that number of bytes. On average, we will have one or two comparisons, compared to 6 for finding the floor of the base 2 log.

We could probably try the same trick at the nybble level, if we're getting lots of numbers in the 1-128 range, which should show an improvement over the byte encoding.


chunkDecode

public final int chunkDecode()
A chunk decoder. Unary decode the number of bytes and then direct decode the number


byteEncode

public final int byteEncode(int n)
Encodes an integer in a byte-aligned fashion, using the minimal number of bytes. The basic idea: use the 7 lower order bits of a byte to encode a number. If the 8th bit is 0, then there are no further bytes in this number. If the 8th bit is one, then the next byte continues the number. Note that this means that a number that would completly fill an int would take 5 bytes to encode. The hope is that we won't get numbers this big.

Specified by:
byteEncode in interface IntEncoder

byteDecode

public final int byteDecode()
Decodes an integer stored using the byte encoding.

Specified by:
byteDecode in interface IntEncoder
Returns:
the decoded integer.
See Also:
byteEncode(int)

gammaEncode

public int gammaEncode(int n)
Gamma encode an int onto the end of the buffer. For a positive integer n if fl=floor(log2(n)), then the gamma encoding of n is the unary encoding of fl+1 followed by the direct encoding of the difference n-2fl in fl bits.

Parameters:
n - The int to encode
Returns:
The number of bits used in the encoding.

gammaDecode

public int gammaDecode()
Gamma decode the head of the buffer.

Returns:
the decoded number.

deltaEncode

public int deltaEncode(int n)
Delta encode an int onto the end of the buffer. For a positive integer n if fl=floor(log2(n)), then the delta encoding of n is the gamma encoding of fl+1, followedby the direct encoding of the difference n-2fl in fl bits.

Parameters:
n - The int to encode.
Returns:
the number of bits used in the encoding.

deltaDecode

public int deltaDecode()
Delta decode an int from the front of the buffer.

Returns:
the decoded int.

encodeChars

public int encodeChars(char[] a)
Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner.

First, we write the number of bytes in the encoding, using either 10, 18, or 34 bits. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for the character.

This code is based on that in DataOutputStream.

Parameters:
a - The array of characters to encode.
Returns:
The number of bits used in the encoding.

encodeUTFLen

public int encodeUTFLen(int n)
Encodes a number of bytes in a manner suitable for use by decodeChars. The number of bytes is encoded in 10, 16, or 34 bits. depending on the magnitude of the number.

Parameters:
n - The number of bytes to encode.
Returns:
The number of bits used in the encoding.

encodeChars

public int encodeChars(char[] a,
                       int b,
                       int e)
Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner. We first encode the length of the encoded characters, followed by the UTF-8 encodings for the characters.

Parameters:
a - The array of characters to encode.
b - The beginning offset in the array
e - The exclusive ending offset in the array.
Returns:
The number of bits used to encode the array.

encodeChars

public static int encodeChars(char[] a,
                              int b,
                              int e,
                              BitBuffer buff)
Encodes an array of characters using a modified UTF-8 encoding in a machine independant manner. This method does not encode the length of the string onto the front of the buffer. In order to do that you must use the three argument version. This method is intended for cases where you want to encode characters without the final length of the string begin known.

First, we write the number of bytes in the encoding, using either 10, 18, or 34 bits. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for the character.

This code is based on that in DataOutputStream.

Parameters:
a - The array of characters to encode.
b - The beginning offset in the array
e - The exclusive ending offset in the array.
Returns:
The number bytes in the UTF encoding.
See Also:
encodeChars(char[], int, int)

decodeChars

public char[] decodeChars()
Decodes an array of characters from the head of the buffer.

Returns:
A newly allocated array of characters.

decodeChars

public char[] decodeChars(int utflen)

decodeChars

public void decodeChars(int utflen,
                        CACont str)

encodeUTF

public int encodeUTF(java.lang.String s)
Encodes a string using a modified UTF-8 encoding in a machine independant manner.

Parameters:
s - The string to encode.
Returns:
The number of bits used in the encoding.
See Also:
encodeChars(char[])

decodeUTF

public java.lang.String decodeUTF()
Decodes a string from the head of the buffer.

Returns:
The decoded string.

differenceEncodeArray

public int differenceEncodeArray(int n,
                                 int[] a)
Encodes an array integers using a standard difference method. The integers will be returned in sorted order.


differenceDecodeArray

public int[] differenceDecodeArray()
Decodes an array of integers using the standard difference method.


encodeInt

public int encodeInt(int n)
Encodes any Java int.

Parameters:
n - The int to encode.
Returns:
The number of bits used to encode the number.

decodeInt

public int decodeInt()
Decodes any Java int.


encodeLong

public int encodeLong(long n)
Encodes any Java long.


decodeLong

public long decodeLong()
Decodes any Java long.


append

public void append(BitBuffer b)
Append another BitBuffer to this one.

Parameters:
b - The BitBuffer to append.

copyNBitsFrom

public void copyNBitsFrom(BitBuffer b,
                          int n)
Copies n bits from the given buffer onto our buffer.

Parameters:
b - The BitBuffer to copy bits from.
n - The number of bits to copy.

or

public void or(BitBuffer r)
Calculate the bitwise or of this buffer and another.

Parameters:
r - The BitBuffer to or with this one.

and

public void and(BitBuffer r)
Calculate the bitwise and of this buffer and another.

Parameters:
r - The BitBuffer to and with this one.

xor

public void xor(BitBuffer r)
Calculate the bitwise xor of this buffer and another.

Parameters:
r - the BitBuffer to xor with this one.

complement

public void complement()
Calculate the bitwise complement of this buffer.


write

public void write(BitBuffer b)
Write this BitBuffer to another.


write

public void write(java.io.DataOutput dout)
           throws java.io.IOException
Write a buffer to a stream with the length.

Throws:
java.io.IOException

write

public void write(java.io.DataOutput dout,
                  boolean writeLength)
           throws java.io.IOException
Write a BitBuffer to a class implementing DataOuput. If the second parameter is true, then the length of the buffer will be written out first as a 4 byte integer.

If the second parameter is false, then the length will not be written out. This option is meant to be used in situations where we want to batch up writes to provide better performance. The proviso is the user must write the number of bits to read in to the head of the string. If this is not done, you will not be able to read the BitBuffer back in.

Parameters:
dout - The DataOutput object to write to.
writeLength - If true, the length of the buffer will be written out first as a 32 bit number.
Throws:
java.io.IOException - if the write fails.

read

public void read(java.io.DataInput din)
          throws java.io.IOException
Read a BitBuffer from an object that implements DataInput. This will allow us to use BitBuffers with random access files.

Parameters:
din - The DataInput object to read from.
Throws:
java.io.IOException - if the read fails.

skip

public static void skip(java.io.DataInput in)
                 throws java.io.IOException
Skips a bit buffer without reading it in.

Throws:
java.io.IOException

unitToLogicalString

protected java.lang.String unitToLogicalString(int unit,
                                               int pos)
Get the bits in a unit from right to left, put them in a string left to right, starting at the given position.

Parameters:
unit - the unit to process
pos - the place to start.
Returns:
A String cotaining the bits in the unit.

intToBinaryString

protected static java.lang.String intToBinaryString(int n)
Build a string representation of an int with the bits in the right order.

Parameters:
n - The number to represent

longToBinaryString

protected static java.lang.String longToBinaryString(long n)

toActualString

public java.lang.String toActualString()
Print the bits in the buffer in the order in which they actually occur.


toLogicalString

public java.lang.String toLogicalString()
Provide a logical string representation of the bit buffer. A logical representation shows the bits in the buffer from left to right as you would expect them.

Returns:
the representation.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object