org.edg.data.util
Class BitUtils

java.lang.Object
  |
  +--org.edg.data.util.BitUtils

public abstract class BitUtils
extends java.lang.Object

A set of helper methods useful when working with byte[] that actually need to be manipulated on the bit level, not the byte level. We support bit arrays up to 2^^31 bits long. We assume that range checking on the array has already been done for indexes; we just throw the underlying IndexOutOfBoundsException.

Version:
$ Id:$
Author:
James Casey

Method Summary
static int bitsToBytes(int bits)
          return the number of bytes needed to hold this number of bits.
static java.lang.String byteArrayToString(byte[] bits, int numBits)
          given a bit bits, turn it into a string of bits
static void clearBit(byte[] bits, int bit)
          clear a bit in the bit array
static byte decrHighNybble(byte b)
          decrement the high nybble, treating it a an unsigned nybble.
static byte decrLowNybble(byte b)
          decrement the low nybble, treating it a an unsigned nybble.
static int fourByteToInt(byte b0, byte b1, byte b2, byte b3)
           
static byte highNybble(byte b)
           
static byte incrHighNybble(byte b)
          increment the high nybble, treating it a an unsigned nybble.
static byte incrLowNybble(byte b)
          descrement the high nybble, treating it a an unsigned nybble.
static boolean isSet(byte[] bits, int bit)
          Is a bit set in the bit array.
static byte lowNybble(byte b)
           
static int numBytes(int number)
          return the number of 8 bit bytes needed to represent this positive integer.
static void setBit(byte[] bits, int bit)
          set a bit in the bit array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

numBytes

public static final int numBytes(int number)
return the number of 8 bit bytes needed to represent this positive integer. We treat the paramter as an unsigned int in this calculation.

Parameters:
number - the number
Returns:
the number of bytes needed

bitsToBytes

public static final int bitsToBytes(int bits)
return the number of bytes needed to hold this number of bits. We treat the parameter as an unsigned int in this calculation.

Parameters:
bits - the bit array
Returns:
the number of bytes needed

setBit

public static final void setBit(byte[] bits,
                                int bit)
set a bit in the bit array

Parameters:
bits - the bit array
bit - the bit to set

clearBit

public static final void clearBit(byte[] bits,
                                  int bit)
clear a bit in the bit array

Parameters:
bits - the bit array
bit - the bit to set

isSet

public static final boolean isSet(byte[] bits,
                                  int bit)
Is a bit set in the bit array.

Parameters:
bits - the bit array
bit - the bit to check
Returns:
true if the bit is set

byteArrayToString

public static final java.lang.String byteArrayToString(byte[] bits,
                                                       int numBits)
given a bit bits, turn it into a string of bits

Parameters:
bits - the bit bits
numBits - the number of bits in the bits
Returns:
the bit bits as a string.

fourByteToInt

public static int fourByteToInt(byte b0,
                                byte b1,
                                byte b2,
                                byte b3)

incrHighNybble

public static byte incrHighNybble(byte b)
increment the high nybble, treating it a an unsigned nybble. If it would overflow, do not increment.

Parameters:
b - the byte
Returns:
the incremented byte

decrHighNybble

public static byte decrHighNybble(byte b)
decrement the high nybble, treating it a an unsigned nybble. If it would overflow, do not decrement.

Parameters:
b - the byte
Returns:
the decremented byte

incrLowNybble

public static byte incrLowNybble(byte b)
descrement the high nybble, treating it a an unsigned nybble. If it would overflow, do not decrement.

Parameters:
b - the byte
Returns:
the incremented byte

decrLowNybble

public static byte decrLowNybble(byte b)
decrement the low nybble, treating it a an unsigned nybble. If it would overflow, do not decrement.

Parameters:
b - the byte
Returns:
the decremented byte

lowNybble

public static byte lowNybble(byte b)

highNybble

public static byte highNybble(byte b)