com.alien.enterpriseRFID.util
Class BitMath

java.lang.Object
  extended bycom.alien.enterpriseRFID.util.BitMath

public class BitMath
extends java.lang.Object

Bit Math contains a number of methods for bitwise manipulate of byte data commonly found in RFID scenarios.

Version:
1.1 December 2003
Author:
Chris Parkinson, David Krull

Constructor Summary
BitMath()
           
 
Method Summary
static int generateCRC16Checksum09(byte[] byteArray, int offset, int length)
          Generates a CRC-type checksum used to store an EPC-64 ID.
static int generateCRC16Checksum10(byte[] byteArray, int offset, int length)
          Generates a CRC-type checksum used to store an EPC-64 ID.
static int generateCRC16Checksum10(java.lang.String tagID)
          Generates a CRC-type checksum for a Tag ID string.
static short generateLRC16Checksum(byte[] byteArray, int offset, int length)
          Generates an LRC16-type checksum over a portion of a byteArray.
static byte[] getBitArray(byte[] byteArray)
          Returns an array of bits, given an array of bytes.
static byte[] getBitArray(byte[] byteArray, int offset, int length)
          Get Bit Array from an array of bytes, specifying the offset and length to extract data from within the byte array.
static byte[] getByteArray(byte[] bitArray)
          Get an array of bytes from an array of bits.
static long getValue(byte[] bitArray, int start, int length)
          Return a long value from a bit array.
static byte[] reverseBitArray(byte[] bitArray)
          Reverses the order of a bit array.
static byte[] setValue(byte[] bitArray, long value, int start, int length)
          Inserts values into an existing bit array.
static void validate(java.lang.String valueName, long value, int bitCount)
          Ensures a given number is small enough to fit within a certain number of bits.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitMath

public BitMath()
Method Detail

reverseBitArray

public static byte[] reverseBitArray(byte[] bitArray)
Reverses the order of a bit array. If the input is 10011, the output will be 11001. Both the input parameter and output are an array of bytes, where each byte represents a bit value of either 0 or 1.

Parameters:
bitArray - an array of bytes to be reversed
Returns:
the reversed bit array, as a byte array

getBitArray

public static byte[] getBitArray(byte[] byteArray)
                          throws java.lang.IllegalArgumentException
Returns an array of bits, given an array of bytes. The result will be an array of bytes, where each byte represents a bit with a value of either 0 or 1.

Parameters:
byteArray - the array of bytes to extract data from
Returns:
an array of bits
Throws:
java.lang.IllegalArgumentException - if the byteArray is null

getBitArray

public static byte[] getBitArray(byte[] byteArray,
                                 int offset,
                                 int length)
                          throws java.lang.IllegalArgumentException
Get Bit Array from an array of bytes, specifying the offset and length to extract data from within the byte array. The result will be an array of bytes, where each byte represents a bit with a value of either 0 or 1. The resulting array runs from right to left, MSB first to LSB last.

Parameters:
byteArray - the array of bytes to extract data from
offset - the start offset to start getting bits of data from
length - the number of bytes to include
Returns:
an array of bits as a byte array
Throws:
java.lang.IllegalArgumentException - if the byteArray is null

getByteArray

public static byte[] getByteArray(byte[] bitArray)
                           throws java.lang.IllegalArgumentException
Get an array of bytes from an array of bits. The value passed in is an array of bits, with each having a value of either 0 or 1. The result is a contraction of these bits into and array of bytes. The bit array need not be 8-bit aligned.

Parameters:
bitArray - the array of bits to extract data from
Returns:
an array of bytes
Throws:
java.lang.IllegalArgumentException - if the bitArray is null

getValue

public static long getValue(byte[] bitArray,
                            int start,
                            int length)
                     throws java.lang.IllegalArgumentException
Return a long value from a bit array. Converts a number of consecutive bits in a bit array into a Long value. The length cannot be longer than 64 bits (size of long return value).

Parameters:
bitArray - the array of bits to extract data from
start - the starting Bit, from the left to get data from
length - the number of bits to extract
Returns:
the long representation of the bitArray
Throws:
java.lang.IllegalArgumentException - if the bitArray is null
java.lang.IllegalArgumentException - if the start value is negative or larger than the bitArray's length
java.lang.IllegalArgumentException - if the length start+length is longer than the bitArray's length

setValue

public static byte[] setValue(byte[] bitArray,
                              long value,
                              int start,
                              int length)
                       throws java.lang.IllegalArgumentException
Inserts values into an existing bit array. You pass in the original bitArray, the start and length parameters for the insertion, and the new bit values (as a long). The length cannot be longer than 64 bits (size of long value).

Parameters:
bitArray - the bitArray to set values into
value - a long value containing the data to set
start - the starting bit to insert into, from the left
length - the number of bits to fill in
Returns:
modified bitArray
Throws:
java.lang.IllegalArgumentException - if the bitArray is null
java.lang.IllegalArgumentException - if the start is negative or greater than the bitArray's length
java.lang.IllegalArgumentException - if the start+length is greater than the bitArray's length

validate

public static void validate(java.lang.String valueName,
                            long value,
                            int bitCount)
                     throws java.lang.IllegalArgumentException
Ensures a given number is small enough to fit within a certain number of bits. If the number is negative, or too large to fit, an exception is thrown.

Parameters:
valueName - a description of the value, for use in exception reporting
value - the value to validate
bitCount - the maximum number of bits
Throws:
java.lang.IllegalArgumentException - if the number of out of range

generateLRC16Checksum

public static short generateLRC16Checksum(byte[] byteArray,
                                          int offset,
                                          int length)
Generates an LRC16-type checksum over a portion of a byteArray.

Parameters:
byteArray - the byte array to generate checksum for
offset - the start offset at which to start generating checksum data
length - the number of bytes to include in the checksum
Returns:
the LRC16 checksum

generateCRC16Checksum09

public static int generateCRC16Checksum09(byte[] byteArray,
                                          int offset,
                                          int length)
                                   throws java.lang.IllegalArgumentException
Generates a CRC-type checksum used to store an EPC-64 ID. This checksum corresponds to v0.9 MIT Spec code, where the byte ordering of the tag is [EPC][CRC]

Parameters:
byteArray - the byte array over which to generate checksum
offset - the start offset at which to start generating checksum data
length - the number of bytes to include in the checksum
Returns:
the int checksum
Throws:
java.lang.IllegalArgumentException - if the byteArray is null

generateCRC16Checksum10

public static int generateCRC16Checksum10(byte[] byteArray,
                                          int offset,
                                          int length)
                                   throws java.lang.IllegalArgumentException
Generates a CRC-type checksum used to store an EPC-64 ID. This checksum corresponds to v1.0 MIT Spec code, where the byte ordering of the tag is [CRC][EPC]

Parameters:
byteArray - the byte array over which to generate checksum
offset - the start offset at which to start generating checksum data
length - the number of bytes to include in the checksum
Returns:
the int checksum
Throws:
java.lang.IllegalArgumentException - if the byteArray is null

generateCRC16Checksum10

public static int generateCRC16Checksum10(java.lang.String tagID)
                                   throws java.lang.IllegalArgumentException
Generates a CRC-type checksum for a Tag ID string.

Parameters:
tagID - the string tag ID
Returns:
the int checksum
Throws:
java.lang.IllegalArgumentException - if the tagID is empty or null