genopt.algorithm.util.math
Class Binary

java.lang.Object
  extended by genopt.algorithm.util.math.Binary

public class Binary
extends java.lang.Object

Mathematical functions for binary numbers.

This project was carried out at:

and supported by

GenOpt Copyright (c) 1998-2011, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.

Version:
GenOpt(R) 3.1.0 (December 8, 2011)

Author:
Michael Wetter

Field Summary
private static int BIT_SIZE
          The number of bits used to present binary numbers
 
Constructor Summary
private Binary()
          Empty constructor.
 
Method Summary
private static long binaryToLong(char[] charArray)
          Converts a binary char array to the long number it presents.
private static long binaryToLong(int[] intArray)
          Converts a binary int array to the long number it presents.
private static java.lang.String binaryToString(int[] intArr, int length)
          Returns a String representation of the argument that contains 0 or 1.
static long getGrayCode(long n)
          Gets the Gray code.
static long getInverseGrayCode(int[] gray)
          Gets the inverse of the Gray code of a binary int array.
static long getInverseGrayCode(long gray)
          Gets the inverse of the Gray code.
static long getL2Norm(int[] i, int[] j)
          Gets the L2 norm.
static int getStringLength(long n)
          Gets the length of the binary string required to represent the argument.
static long ieor(long i, long j)
          Computes the value of a bitwise exclusive or between the arguments.
static char[] increaseLength(char[] binStr, int length)
          Increases the length of a char array by setting additional elements with 0.
static long ishft(long i, int shift)
          Computes the value obtained by shifting the bits of i by shift positions.
static void main(java.lang.String[] args)
           
static char[] toBinaryChar(long i)
          Returns a char[] representation of the long argument as an unsigned integer in base 2.
private static int[] toBinaryInt(char[] c)
          Converts a char array with binary values to a binary int array.
static int[] toBinaryInt(long i)
          Returns an int[] representation of the long argument as an unsigned integer in base 2.
static int[] toBinaryInt(long i, int length)
          Returns an int[] representation of the long argument as an unsigned integer in base 2.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIT_SIZE

private static final int BIT_SIZE
The number of bits used to present binary numbers

See Also:
Constant Field Values
Constructor Detail

Binary

private Binary()
Empty constructor. This class has only static methods and fields.

Method Detail

getL2Norm

public static final long getL2Norm(int[] i,
                                   int[] j)
Gets the L2 norm.

Parameters:
i - an integer array
j - an integer array
Returns:
the L2 norm
Throws:
java.lang.IllegalArgumentException - if the arguments have different length

toBinaryChar

public static char[] toBinaryChar(long i)
Returns a char[] representation of the long argument as an unsigned integer in base 2.

Parameters:
i - a long to be converted to a char[].
Returns:
the char[] representation of the unsigned long value represented by the argument in binary (base 2).

toBinaryInt

public static int[] toBinaryInt(long i)
Returns an int[] representation of the long argument as an unsigned integer in base 2.

Parameters:
i - a long to be converted to an int[].
Returns:
the int[] representation of the unsigned long value represented by the argument in binary (base 2).

toBinaryInt

public static int[] toBinaryInt(long i,
                                int length)
Returns an int[] representation of the long argument as an unsigned integer in base 2.

Parameters:
i - a long to be converted to an int[].
length - number of elements of the returned array
Returns:
the int[] representation of the unsigned long value represented by the argument in binary (base 2).
Throws:
java.lang.IllegalArgumentException - if length is not long enough to represent the argument as a binary integer array

getStringLength

public static int getStringLength(long n)
                           throws java.lang.IllegalArgumentException
Gets the length of the binary string required to represent the argument.

Parameters:
n - argument whose required length will be computed.
Returns:
the length of the binary string required to represent the argument.
Throws:
java.lang.IllegalArgumentException - if n < 0

binaryToString

private static final java.lang.String binaryToString(int[] intArr,
                                                     int length)
                                              throws java.lang.IllegalArgumentException
Returns a String representation of the argument that contains 0 or 1.

Parameters:
intArr - an int[] array containing 0 or 1
length - the length of the string array that will be returned
Returns:
the String representation of the argument
Throws:
java.lang.IllegalArgumentException - if the argument contains non-binary numbers

getGrayCode

public static long getGrayCode(long n)
                        throws java.lang.IllegalArgumentException
Gets the Gray code.

Parameters:
n - non-negative argument whose length will be computed
Returns:
the Gray code
Throws:
java.lang.IllegalArgumentException - if n < 0

toBinaryInt

private static int[] toBinaryInt(char[] c)
Converts a char array with binary values to a binary int array.

Parameters:
c - a char array with 0 and 1 elements
Returns:
c converted to an int array
Throws:
java.lang.IllegalArgumentException - if c is not a binary array

binaryToLong

private static long binaryToLong(int[] intArray)
Converts a binary int array to the long number it presents.

Parameters:
intArray - a binary int array
Returns:
the long number that is presented by the argument

binaryToLong

private static long binaryToLong(char[] charArray)
                          throws java.lang.IllegalArgumentException
Converts a binary char array to the long number it presents.

Parameters:
charArray - a binary int array
Returns:
the long number that is presented by the argument
Throws:
java.lang.IllegalArgumentException - if c is not a binary array

getInverseGrayCode

public static long getInverseGrayCode(int[] gray)
Gets the inverse of the Gray code of a binary int array. gray is first encoded in a long value, and then the inverse Gray coding of the long is computed and returned.

Parameters:
gray - an int[] argument with binary numbers
Returns:
the inverse of gray, using inverse Gray coding

getInverseGrayCode

public static long getInverseGrayCode(long gray)
Gets the inverse of the Gray code.

Parameters:
gray - a number in Gray code
Returns:
the inverse of gray, using inverse Gray coding

increaseLength

public static final char[] increaseLength(char[] binStr,
                                          int length)
                                   throws java.lang.IllegalArgumentException
Increases the length of a char array by setting additional elements with 0. If n=binStr.length < length , then the return argument has 0 at the elements 0 to lenth-n.

Parameters:
binStr - the binary string string to be extended
length - the length of the returned binary string
Returns:
binStr with increased length
Throws:
java.lang.IllegalArgumentException - if binStr.length > length

ieor

public static final long ieor(long i,
                              long j)
Computes the value of a bitwise exclusive or between the arguments.

Parameters:
i - first argument
j - second argument
Returns:
the value of a bitwise exclusive or between the arguments

ishft

public static final long ishft(long i,
                               int shift)
Computes the value obtained by shifting the bits of i by shift positions. If shift is positive, the shift is to the left, otherwise to the right. Bits shifted off the left or right are lost and zero bits are shifted in from the opposite end.

Parameters:
i - value whose bits will be shifted
shift - number of positions that the bits will be shifted
Returns:
the value of i after shifting the bits

main

public static void main(java.lang.String[] args)