Sharkysoft home

lava.util
Class BitTwiddler

java.lang.Object
  |
  +--lava.util.BitTwiddler

public class BitTwiddler
extends java.lang.Object

Bit-twiddling operations.

BitTwiddler is a collection of static methods that perform bit-twiddling operations.

Version:
1999.08.08

Constructor Summary
BitTwiddler()
           
 
Method Summary
static java.lang.String bitString(int value, int blocksize)
          Represents an int in binary notation as a string of zeros and ones.
static int countSetBits(byte b)
          Counts set bits.
static int countSetBits(int i)
          Counts set bits.
static int countSetBits(long l)
          Counts set bits.
static int countSetBits(short s)
          Counts set bits.
static int mostSignificant(int integer, boolean value)
          Searches for a bit value in an int.
static int reverseBits(int i)
          Reverses order of bits in ing.
static long reverseBits(long l)
          Reverses order of bits in long.
static int reverseEndian(int i)
          Reverses endianness of int.
static long reverseEndian(long l)
          Reverses endianness of long.
static short reverseEndian(short s)
          Reverses endianness of short.
static byte[] toBytesBe(int i, byte[] dest, int offset)
          Breaks int into big endian bytes.
static byte[] toBytesBe(long l, byte[] dest, int offset)
          Breaks long into big endian bytes.
static byte[] toBytesBe(short s, byte[] dest, int offset)
          Breaks short into big endian bytes.
static byte[] toBytesLe(int i, byte[] dest, int offset)
          Breaks int into little endian bytes.
static byte[] toBytesLe(long l, byte[] dest, int offset)
          Breaks long into little endian bytes.
static byte[] toBytesLe(short s, byte[] dest, int offset)
          Breaks short into little endian bytes.
static int toIntBe(byte[] b)
          Shorthand for toIntBe (b, 0).
static int toIntBe(byte[] b, int offset)
          Converts big endian bytes into int.
static int toIntLe(byte[] b)
          Shorthand for toIntLe (b, 0).
static int toIntLe(byte[] b, int offset)
          Converts little endian bytes into int.
static long toLongBe(byte[] b)
          Shorthand for toLongBe (b, 0).
static long toLongBe(byte[] b, int offset)
          Converts big endian bytes into long.
static long toLongLe(byte[] b)
          Shorthand for toLongLe (b, 0).
static long toLongLe(byte[] b, int offset)
          Converts little endian bytes into long.
static short toShortBe(byte[] b)
          Shorthand for toShortBe (b, 0).
static short toShortBe(byte[] b, int offset)
          Converts big endian bytes into short.
static short toShortLe(byte[] b)
          Shorthand for toShortLe (b, 0).
static short toShortLe(byte[] b, int offset)
          Converts little endian bytes into short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitTwiddler

public BitTwiddler()
Method Detail

toLongBe

public static final long toLongBe(byte[] b,
                                  int offset)
Converts big endian bytes into long.

Details: This method converts big endian ordered bytes into a long. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the long
Since:
1999.08.01

toLongBe

public static final long toLongBe(byte[] b)
Shorthand for toLongBe (b, 0).
Since:
1998.11.19
See Also:
toLongBe(byte[], int)

toLongLe

public static final long toLongLe(byte[] b,
                                  int offset)
Converts little endian bytes into long.

Details: This method converts little endian ordered bytes into a long. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the long
Since:
1999.08.01

toLongLe

public static final long toLongLe(byte[] b)
Shorthand for toLongLe (b, 0).
Since:
1998.11.19
See Also:
toLongLe(byte[], int)

toIntBe

public static final int toIntBe(byte[] b,
                                int offset)
Converts big endian bytes into int.

Details: This method converts big endian ordered bytes into an int. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the int
Since:
1999.08.01

toIntBe

public static final int toIntBe(byte[] b)
Shorthand for toIntBe (b, 0).
Since:
1998.11.19
See Also:
toIntBe(byte[], int)

toIntLe

public static final int toIntLe(byte[] b,
                                int offset)
Converts little endian bytes into int.

Details: This method converts little endian ordered bytes into an int. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the int
Since:
1999.08.01

toIntLe

public static final int toIntLe(byte[] b)
Shorthand for toIntLe (b, 0).
Since:
1998.11.19
See Also:
toIntLe(byte[], int)

toShortBe

public static final short toShortBe(byte[] b,
                                    int offset)
Converts big endian bytes into short.

Details: This method converts big endian ordered bytes into a short. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the short
Since:
1999.08.01

toShortBe

public static final short toShortBe(byte[] b)
Shorthand for toShortBe (b, 0).
Since:
1998.11.19
See Also:
toShortBe(byte[], int)

toShortLe

public static final short toShortLe(byte[] b,
                                    int offset)
Converts little endian bytes into short.

Details: This method converts little endian ordered bytes into a short. offset is the array index of the first byte.

Parameters:
b - the byte array
the - array offset (default value: 0)
Returns:
the short
Since:
1999.08.01

toShortLe

public static final short toShortLe(byte[] b)
Shorthand for toShortLe (b, 0).
Since:
1998.11.19
See Also:
toShortLe(byte[], int)

toBytesBe

public static final byte[] toBytesBe(long l,
                                     byte[] dest,
                                     int offset)
Breaks long into big endian bytes.

Details: This method breaks a long into 8 bytes and stores them in the given array, at the given index, using big endian byte ordering.

Parameters:
l - the long
dest - the destination array
offset - the array index
Returns:
dest

toBytesLe

public static final byte[] toBytesLe(long l,
                                     byte[] dest,
                                     int offset)
Breaks long into little endian bytes.

Details: This method breaks a long into 8 bytes and stores them in the given array, at the given index, using little endian byte ordering.

Parameters:
l - the long
dest - the destination array
offset - the array index
Returns:
dest

toBytesBe

public static final byte[] toBytesBe(int i,
                                     byte[] dest,
                                     int offset)
Breaks int into big endian bytes.

Details: This method breaks an int into 4 bytes and stores them in the given array, at the given index, using big endian byte ordering.

Parameters:
i - the int
dest - the destination array
offset - the array index
Returns:
dest

toBytesLe

public static final byte[] toBytesLe(int i,
                                     byte[] dest,
                                     int offset)
Breaks int into little endian bytes.

Details: This method breaks an int into 4 bytes and stores them in the given array, at the given index, using little endian byte ordering.

Parameters:
i - the int
dest - the destination array
offset - the array index
Returns:
dest

toBytesBe

public static final byte[] toBytesBe(short s,
                                     byte[] dest,
                                     int offset)
Breaks short into big endian bytes.

Details: This method breaks a short into 2 bytes and stores them in the given array, at the given index, using big endian byte ordering.

Parameters:
s - the short
dest - the destination array
offset - the array index
Returns:
dest

toBytesLe

public static final byte[] toBytesLe(short s,
                                     byte[] dest,
                                     int offset)
Breaks short into little endian bytes.

Details: This method breaks a short into 2 bytes and stores them in the given array, at the given index, using little endian byte ordering.

Parameters:
s - the short
dest - the destination array
offset - the array index
Returns:
dest

reverseEndian

public static final long reverseEndian(long l)
Reverses endianness of long.
Parameters:
l - the long
Returns:
the reversed long

reverseEndian

public static final int reverseEndian(int i)
Reverses endianness of int.
Parameters:
i - the int
Returns:
the reversed int

reverseEndian

public static final short reverseEndian(short s)
Reverses endianness of short.
Parameters:
s - the short
Returns:
the reversed short

reverseBits

public static final long reverseBits(long l)
Reverses order of bits in long.
Parameters:
l - the long
Returns:
the reversed long

reverseBits

public static final int reverseBits(int i)
Reverses order of bits in ing.
Parameters:
i - the int
Returns:
the reversed int

bitString

public static java.lang.String bitString(int value,
                                         int blocksize)
Represents an int in binary notation as a string of zeros and ones. The binary string may be broken up into blocks of digits to increase readability.
Parameters:
value - the value to represent
blocksize - the number digits per block
Returns:
a binary representation of value

mostSignificant

public static final int mostSignificant(int integer,
                                        boolean value)
Searches for a bit value in an int. Returns the position of the most significant bit matching the given value, or -1 if the bit value does not occur at all in integer.
Parameters:
integer - the int
Returns:
the position

countSetBits

public static final int countSetBits(long l)
Counts set bits.

Details: This method returns the number of bits in l that are 1.

Parameters:
l - a long
Returns:
the count
Since:
1999.08.08

countSetBits

public static final int countSetBits(int i)
Counts set bits.

Details: This method returns the number of bits in i that are 1.

Parameters:
i - an int
Returns:
the count
Since:
1999.08.08

countSetBits

public static final int countSetBits(short s)
Counts set bits.

Details: This method returns the number of bits in s that are 1.

Parameters:
s - a short
Returns:
the count
Since:
1999.08.08

countSetBits

public static final int countSetBits(byte b)
Counts set bits.

Details: This method returns the number of bits in b that are 1.

Parameters:
b - a byte
Returns:
the count
Since:
1999.08.08

Sharkysoft home