Package com.tangosol.util
Class BitHelper
java.lang.Object
com.tangosol.util.Base
com.tangosol.util.BitHelper
- Direct Known Subclasses:
ExternalizableHelper
A collection of methods for bit-based operations.
- Author:
- cp 2006.01.10 starting from the MemberSet implementation
-
Nested Class Summary
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
countBits
(byte b) Count the number of bits set in the passed integral value.static int
countBits
(int n) Count the number of bits set in the passed integral value.static int
countBits
(long l) Count the number of bits set in the passed integral value.static int
indexOfLSB
(byte b) Determine the least significant bit of the passed integral value.static int
indexOfLSB
(int n) Determine the least significant bit of the passed integral value.static int
indexOfLSB
(long l) Determine the least significant bit of the passed integral value.static int
indexOfMSB
(byte b) Determine the most significant bit of the passed integral value.static int
indexOfMSB
(int n) Determine the most significant bit of the passed integral value.static int
indexOfMSB
(long l) Determine the most significant bit of the passed integral value.static byte
rotateLeft
(byte b, int cBits) Rotate the bits of the passed byte value to the left by the number of bits specified.static int
rotateLeft
(int n, int cBits) Rotate the bits of the passed int value to the left by the number of bits specified.static long
rotateLeft
(long n, int cBits) Rotate the bits of the passed long value to the left by the number of bits specified.static byte
rotateRight
(byte b, int cBits) Rotate the bits of the passed byte value to the right by the number of bits specified.static int
rotateRight
(int n, int cBits) Rotate the bits of the passed int value to the right by the number of bits specified.static long
rotateRight
(long n, int cBits) Rotate the bits of the passed long value to the right by the number of bits specified.static String
toBitString
(byte b) Convert a byte to a String of ones and zeros.static String
toBitString
(int n) Convert an int to a String of ones and zeros.static String
toBitString
(long l) Convert a long to a String of ones and zeros.static byte[]
toBytes
(int l) Convert the specified int into a byte array containing a series of eight bytes in big-endian form (MSB first).static void
toBytes
(int n, byte[] ab, int of) Convert the specified int into a series of eight bytes, and write them to the specified byte-array in big-endian form (MSB first).static byte[]
toBytes
(long l) Convert the specified long into a byte array containing a series of eight bytes in big-endian form (MSB first).static void
toBytes
(long l, byte[] ab, int of) Convert the specified long into a series of eight bytes, and write them to the specified byte-array in big-endian form (MSB first).static int
toInt
(byte[] ab) Return the int represented by the sequence of eight bytes (in big-endian form) in the specified byte-array.static int
toInt
(byte[] ab, int of) Return the int represented by the sequence of eight bytes (in big-endian form) in the specified byte-array starting at the given offset.static long
toLong
(byte[] ab) Return the long represented by the sequence of eight bytes (in big-endian form) in the specified byte-array.static long
toLong
(byte[] ab, int of) Return the long represented by the sequence of eight bytes (in big-endian form) in the specified byte-array starting at the given offset.Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
Constructor Details
-
BitHelper
public BitHelper()
-
-
Method Details
-
rotateLeft
public static byte rotateLeft(byte b, int cBits) Rotate the bits of the passed byte value to the left by the number of bits specified.- Parameters:
b
- a byte valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
rotateRight
public static byte rotateRight(byte b, int cBits) Rotate the bits of the passed byte value to the right by the number of bits specified.- Parameters:
b
- a byte valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
rotateLeft
public static int rotateLeft(int n, int cBits) Rotate the bits of the passed int value to the left by the number of bits specified.- Parameters:
n
- an int valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
rotateRight
public static int rotateRight(int n, int cBits) Rotate the bits of the passed int value to the right by the number of bits specified.- Parameters:
n
- an int valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
rotateLeft
public static long rotateLeft(long n, int cBits) Rotate the bits of the passed long value to the left by the number of bits specified.- Parameters:
n
- a long valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
rotateRight
public static long rotateRight(long n, int cBits) Rotate the bits of the passed long value to the right by the number of bits specified.- Parameters:
n
- a long valuecBits
- the number of bit rotations to perform- Returns:
- the value with its bits rotated as indicated
- Since:
- Coherence 3.5
-
countBits
public static int countBits(byte b) Count the number of bits set in the passed integral value.- Parameters:
b
- a byte- Returns:
- the number of bits set in the byte [0..8]
-
countBits
public static int countBits(int n) Count the number of bits set in the passed integral value.- Parameters:
n
- an int- Returns:
- the number of bits set in the int [0..32]
-
countBits
public static int countBits(long l) Count the number of bits set in the passed integral value.- Parameters:
l
- a long- Returns:
- the number of bits set in the long [0..64]
-
indexOfMSB
public static int indexOfMSB(byte b) Determine the most significant bit of the passed integral value.- Parameters:
b
- a byte- Returns:
- -1 if no bits are set; otherwise, the bit position p of the most significant bit such that 1 << p is the most significant bit of b
-
indexOfMSB
public static int indexOfMSB(int n) Determine the most significant bit of the passed integral value.- Parameters:
n
- an int- Returns:
- -1 if no bits are set; otherwise, the bit position p of the most significant bit such that 1 << p is the most significant bit of n
-
indexOfMSB
public static int indexOfMSB(long l) Determine the most significant bit of the passed integral value.- Parameters:
l
- a long- Returns:
- -1 if no bits are set; otherwise, the bit position p of the most significant bit such that 1 << p is the most significant bit of l
-
indexOfLSB
public static int indexOfLSB(byte b) Determine the least significant bit of the passed integral value.- Parameters:
b
- a byte- Returns:
- -1 if no bits are set; otherwise, the bit position p of the least significant bit such that 1 << p is the least significant bit of b
-
indexOfLSB
public static int indexOfLSB(int n) Determine the least significant bit of the passed integral value.- Parameters:
n
- an int- Returns:
- -1 if no bits are set; otherwise, the bit position p of the least significant bit such that 1 << p is the least significant bit of n
-
indexOfLSB
public static int indexOfLSB(long l) Determine the least significant bit of the passed integral value.- Parameters:
l
- a long- Returns:
- -1 if no bits are set; otherwise, the bit position p of the least significant bit such that 1 << p is the least significant bit of l
-
toBitString
Convert a byte to a String of ones and zeros.- Parameters:
b
- a byte- Returns:
- a String of ones and zeros representing the byte value
-
toBitString
Convert an int to a String of ones and zeros.- Parameters:
n
- an int- Returns:
- a String of ones and zeros representing the int value
-
toBitString
Convert a long to a String of ones and zeros.- Parameters:
l
- a long- Returns:
- a String of ones and zeros representing the long value
-
toBytes
public static void toBytes(int n, byte[] ab, int of) Convert the specified int into a series of eight bytes, and write them to the specified byte-array in big-endian form (MSB first).- Parameters:
n
- the int to convert to bytesab
- the byte array to write intoof
- the starting offset- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is shorter thanof + 4
-
toBytes
public static byte[] toBytes(int l) Convert the specified int into a byte array containing a series of eight bytes in big-endian form (MSB first).- Parameters:
l
- the int to convert to bytes- Returns:
- a byte[] representing the big-endian representation of the int
-
toBytes
public static void toBytes(long l, byte[] ab, int of) Convert the specified long into a series of eight bytes, and write them to the specified byte-array in big-endian form (MSB first).- Parameters:
l
- the long to convert to bytesab
- the byte array to write intoof
- the starting offset- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is shorter thanof + 8
-
toBytes
public static byte[] toBytes(long l) Convert the specified long into a byte array containing a series of eight bytes in big-endian form (MSB first).- Parameters:
l
- the long to convert to bytes- Returns:
- a byte[] representing the big-endian representation of the long
-
toInt
public static int toInt(byte[] ab, int of) Return the int represented by the sequence of eight bytes (in big-endian form) in the specified byte-array starting at the given offset.- Parameters:
ab
- the byte-arrayof
- the offset- Returns:
- the int represented by the sequence of bytes
- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is less thanof + 4
-
toInt
public static int toInt(byte[] ab) Return the int represented by the sequence of eight bytes (in big-endian form) in the specified byte-array.- Parameters:
ab
- the byte-array- Returns:
- the int represented by the byte-array
- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is less than4
-
toLong
public static long toLong(byte[] ab, int of) Return the long represented by the sequence of eight bytes (in big-endian form) in the specified byte-array starting at the given offset.- Parameters:
ab
- the byte-arrayof
- the offset- Returns:
- the long represented by the sequence of bytes
- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is less thanof + 8
-
toLong
public static long toLong(byte[] ab) Return the long represented by the sequence of eight bytes (in big-endian form) in the specified byte-array.- Parameters:
ab
- the byte-array- Returns:
- the long represented by the byte-array
- Throws:
ArrayIndexOutOfBoundsException
- iff the length of the byte array is less than8
-