danger.util
Class BitUtils

java.lang.Object
  extended by danger.util.BitUtils


public final class BitUtils
extends Object

Utilities for dealing with byte[]s as bit vectors.


Method Summary
static boolean get(byte[] barr, int n)
          Get the nth bit in the given byte[].
static byte[] newBitVector(int bitCount)
          Construct a new byte[] initially capable of holding bitCount bits.
static void set(byte[] barr, int n, boolean value)
          Set the nth bit in the given byte[] to the given value.
static byte[] stretchSet(byte[] barr, int n, boolean value)
          Set the nth bit in the given byte[] to the given value, making a new larger array to hold the bit if necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newBitVector

public static byte[] newBitVector(int bitCount)
Construct a new byte[] initially capable of holding bitCount bits.

Parameters:
bitCount - the number of bits to hold
Returns:
non-null; an appropriately-allocated byte[]

set

public static void set(byte[] barr,
                       int n,
                       boolean value)
Set the nth bit in the given byte[] to the given value.

Parameters:
barr - non-null; the array to set in
n - which bit to set
value - the value for the bit
Throws:
NullPointerException - thrown if barr == null
IndexOutOfBoundsException - thrown if n is out of range

stretchSet

public static byte[] stretchSet(byte[] barr,
                                int n,
                                boolean value)
Set the nth bit in the given byte[] to the given value, making a new larger array to hold the bit if necessary. (A new array is always allocated if barr is passed as null.)

Parameters:
barr - null-ok; the array to set in
n - which bit to set
value - the value for the bit
Returns:
non-null; the array in which the bit was set (which is different than barr if barr is null or too small)
Throws:
IndexOutOfBoundsException - thrown if n < 0

get

public static boolean get(byte[] barr,
                          int n)
Get the nth bit in the given byte[]. This is lenient and will simply return false if n is out of range or barr is null.

Parameters:
barr - null-ok; the array to query
n - which bit to get
Returns:
the value for the bit