Class Bits
long. This class provides common operations such as
checking, setting, clearing, toggling individual bits or masks, identifying
the positions of the first or highest set bit, counting bits, and
constraining a mask to a fixed width.
All operations treat bit index 0 as the least significant bit (LSB)
of the 64-bit word and bit index 63 as the most significant bit.
Since long is inherently unsigned in its bit representation, all
operations use Java’s standard two's-complement bitwise semantics.
This class is final with a private constructor because it is intended
purely as a static utility library.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheck(long bits, long mask) Tests whether all bits specified bymaskare set to1in the given value.static booleancheckAt(long bits, int index) Tests whether the bit at the specified index is set.static longclear(long bits, long mask) Returns a value in which all bits specified bymaskhave been cleared (set to0), leaving all other bits unchanged.static longclearAt(long bits, int index) Clears the bit at the specified index (sets it to0).static intcount(int bits) Counts and returns the number of set bits in the given value.static intcount(long bits) Counts and returns the number of set bits in the given word.static intfirstSet(long bits) Returns the index (0-based) of the lowest set bit (LSB-first).static inthighestSet(long bits) Returns the index (0-based) of the highest set bit (MSB-first).static booleanisEmpty(long bits) Determines whether the given word contains no set bits.static longmask(long bits, int width) Returns the value ofbitstruncated to a field of widthwidthbits (from the least significant side).static longset(long bits, long mask) Returns a value in which all bits specified bymaskhave been set to1, leaving all other bits unchanged.static longsetAt(long bits, int index) Sets the bit at the specified index to1.static longtoggle(long bits, long mask) Returns a value in which all bits specified bymaskhave been toggled (flipped from0to1or1to0).static longtoggleAt(long bits, int index) Toggles the bit at the specified index.static longunsetAll()Clears all bits and returns0L.
-
Method Details
-
check
public static boolean check(long bits, long mask) Tests whether all bits specified bymaskare set to1in the given value.- Parameters:
bits- the 64-bit word to testmask- the mask of bits to check- Returns:
trueif all masked bits are set,falseotherwise
-
set
public static long set(long bits, long mask) Returns a value in which all bits specified bymaskhave been set to1, leaving all other bits unchanged.- Parameters:
bits- the original 64-bit valuemask- the mask of bits to set- Returns:
- the value with the specified bits set
-
clear
public static long clear(long bits, long mask) Returns a value in which all bits specified bymaskhave been cleared (set to0), leaving all other bits unchanged.- Parameters:
bits- the original valuemask- the mask of bits to clear- Returns:
- the value with the specified bits cleared
-
count
public static int count(int bits) Counts and returns the number of set bits in the given value.- Parameters:
bits- the value to count bits in- Returns:
- number of set bits (range
0–32)
-
toggle
public static long toggle(long bits, long mask) Returns a value in which all bits specified bymaskhave been toggled (flipped from0to1or1to0).- Parameters:
bits- the original valuemask- the mask of bits to toggle- Returns:
- the value with the specified bits toggled
-
setAt
public static long setAt(long bits, int index) Sets the bit at the specified index to1.- Parameters:
bits- the original valueindex- the bit index to set (valid range:0–63)- Returns:
- the value with the selected bit set
- Throws:
IllegalArgumentException- ifindexis outside0..63
-
clearAt
public static long clearAt(long bits, int index) Clears the bit at the specified index (sets it to0).- Parameters:
bits- the original valueindex- the bit index to clear (valid range:0–63)- Returns:
- the value with the selected bit cleared
-
toggleAt
public static long toggleAt(long bits, int index) Toggles the bit at the specified index.- Parameters:
bits- the original valueindex- the bit index to toggle (valid range:0–63)- Returns:
- the value with the selected bit toggled
-
checkAt
public static boolean checkAt(long bits, int index) Tests whether the bit at the specified index is set.- Parameters:
bits- the 64-bit word to inspectindex- the bit index to test (valid range:0–63)- Returns:
trueif the bit is set,falseotherwise
-
firstSet
public static int firstSet(long bits) Returns the index (0-based) of the lowest set bit (LSB-first). That is, it returns the position of the first bit whose value is1.This method is equivalent to
Long.numberOfTrailingZeros(long)whenbits != 0.- Parameters:
bits- the 64-bit word- Returns:
- index of the lowest set bit, or
-1ifbits == 0
-
highestSet
public static int highestSet(long bits) Returns the index (0-based) of the highest set bit (MSB-first). That is, it returns the position of the most significant bit whose value is1.This method is equivalent to:
63 - Long.numberOfLeadingZeros(bits)
- Parameters:
bits- the 64-bit word- Returns:
- index of the highest set bit, or
-1ifbits == 0
-
count
public static int count(long bits) Counts and returns the number of set bits in the given word.This method delegates to
Long.bitCount(long).- Parameters:
bits- the 64-bit word- Returns:
- number of set bits (range
0–64)
-
isEmpty
public static boolean isEmpty(long bits) Determines whether the given word contains no set bits.- Parameters:
bits- the word to test- Returns:
trueif the value is0,falseotherwise
-
unsetAll
public static long unsetAll()Clears all bits and returns0L. This is functionally equivalent to assigning0Ldirectly but provided for symmetry with other helpers.- Returns:
0L
-
mask
public static long mask(long bits, int width) Returns the value ofbitstruncated to a field of widthwidthbits (from the least significant side).If
width <= 0, the method returns0L. Ifwidth >= 64, the method returnsbitsunchanged.Internally this method constructs a low-order mask of the form
(1L << width) - 1and applies it with bitwise AND.- Parameters:
bits- the original valuewidth- number of low bits to preserve (range1–64)- Returns:
- the masked value
-