public class Binary
extends java.lang.Object
This project was carried out at:
and supported byGenOpt Copyright (c) 1998-2021, 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.
Modifier and Type | Field and Description |
---|---|
private static int |
BIT_SIZE
The number of bits used to present binary numbers
|
Modifier | Constructor and Description |
---|---|
private |
Binary()
Empty constructor.
|
Modifier and Type | Method and Description |
---|---|
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. |
private static final int BIT_SIZE
private Binary()
static
methods and fields.public static final long getL2Norm(int[] i, int[] j)
i
- an integer arrayj
- an integer arrayjava.lang.IllegalArgumentException
- if the arguments have different lengthpublic static char[] toBinaryChar(long i)
char[]
representation of the long
argument as an unsigned integer in base 2.i
- a long
to be converted to a char[]
.char[]
representation of the
unsigned long
value represented by the argument
in binary (base 2).public static int[] toBinaryInt(long i)
int[]
representation of the long
argument as an unsigned integer in base 2.i
- a long
to be converted to an int[]
.int[]
representation of the
unsigned long
value represented by the argument
in binary (base 2).public static int[] toBinaryInt(long i, int length)
int[]
representation of the long
argument as an unsigned integer in base 2.i
- a long
to be converted to an int[]
.length
- number of elements of the returned arrayint[]
representation of the
unsigned long
value represented by the argument
in binary (base 2).java.lang.IllegalArgumentException
- if length
is
not long enough to represent the argument as a binary integer arraypublic static int getStringLength(long n) throws java.lang.IllegalArgumentException
n
- argument whose required length will be computed.java.lang.IllegalArgumentException
- if n < 0
private static final java.lang.String binaryToString(int[] intArr, int length) throws java.lang.IllegalArgumentException
String
representation of the argument
that contains 0
or 1
.intArr
- an int[]
array containing 0
or 1
length
- the length of the string array that will be returnedString
representation of the argumentjava.lang.IllegalArgumentException
- if the argument contains non-binary
numberspublic static long getGrayCode(long n) throws java.lang.IllegalArgumentException
n
- non-negative argument whose length will be computedjava.lang.IllegalArgumentException
- if n < 0
private static int[] toBinaryInt(char[] c)
char
array with binary values to
a binary int
array.c
- a char
array with 0
and 1
elementsc
converted to an int
arrayjava.lang.IllegalArgumentException
- if c
is not a binary arrayprivate static long binaryToLong(int[] intArray)
int
array to
the long number it presents.intArray
- a binary int
arrayprivate static long binaryToLong(char[] charArray) throws java.lang.IllegalArgumentException
char
array to
the long number it presents.charArray
- a binary int
arrayjava.lang.IllegalArgumentException
- if c
is not a binary arraypublic static long getInverseGrayCode(int[] gray)
int
array.
gray
is first encoded in a long
value,
and then the inverse Gray coding of the long
is
computed and returned.gray
- an int[]
argument with binary numbersgray
, using inverse
Gray codingpublic static long getInverseGrayCode(long gray)
gray
- a number in Gray codegray
, using inverse
Gray codingpublic static final char[] increaseLength(char[] binStr, int length) throws java.lang.IllegalArgumentException
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
.binStr
- the binary string string to be extendedlength
- the length of the returned binary stringbinStr
with increased lengthjava.lang.IllegalArgumentException
- if binStr.length > length
public static final long ieor(long i, long j)
i
- first argumentj
- second argumentpublic static final long ishft(long i, int shift)
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.i
- value whose bits will be shiftedshift
- number of positions that the bits will be shiftedi
after shifting the bitspublic static void main(java.lang.String[] args)