Package uk.ac.starlink.ttools.func
Class Bits
java.lang.Object
uk.ac.starlink.ttools.func.Bits
Bit manipulation functions.
Note that for bitwise AND, OR, XOR of integer values etc you can use
the java bitwise operators
"&
", "|
", "^
".
- Since:
- 4 Apr 2022
- Author:
- Mark Taylor
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
bitCount
(long i) Returns the number of set bits in the 64-bit two's complement representation of the integer argument.static int
fromBinary
(String binVal) Converts a string representing a binary number to its integer value.static boolean
hasBit
(long value, int bitIndex) Determines whether a given integer has a certain bit set to 1.static String
toBinary
(long value) Converts the integer argument to a binary string consisting only of 1s and 0s.
-
Method Details
-
hasBit
public static boolean hasBit(long value, int bitIndex) Determines whether a given integer has a certain bit set to 1.- Parameters:
value
- integer whose bits are to be testedbitIndex
- index of bit to be tested in range 0..63, where 0 is the least significant bit- Returns:
- true if bit is set; more or less equivalent to
(value & 1L<<bitIndex) != 0
- Examples:
hasBit(64, 6) = true
,hasBit(63, 6) = false
-
bitCount
public static int bitCount(long i) Returns the number of set bits in the 64-bit two's complement representation of the integer argument.- Parameters:
i
- integer value- Returns:
- number of "1" bits in the binary representation of
i
- Examples:
bitCount(64) = 1
,bitCount(3) = 2
-
toBinary
Converts the integer argument to a binary string consisting only of 1s and 0s.- Parameters:
value
- integer value- Returns:
- binary representation of
value
- Examples:
toBinary(42) = "101010"
,toBinary(255^7) = "11111000"
-
fromBinary
Converts a string representing a binary number to its integer value.- Parameters:
binVal
- binary representation of value- Returns:
- integer value represented by binary string
binVal
- Examples:
fromBinary("101010") = 42
-