Class Bits

java.lang.Object
edu.cnu.mdi.util.Bits

public final class Bits extends Object
A compact collection of static utilities for manipulating bit masks stored in a 64-bit 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 Type
    Method
    Description
    static boolean
    check(long bits, long mask)
    Tests whether all bits specified by mask are set to 1 in the given value.
    static boolean
    checkAt(long bits, int index)
    Tests whether the bit at the specified index is set.
    static long
    clear(long bits, long mask)
    Returns a value in which all bits specified by mask have been cleared (set to 0), leaving all other bits unchanged.
    static long
    clearAt(long bits, int index)
    Clears the bit at the specified index (sets it to 0).
    static int
    count(int bits)
    Counts and returns the number of set bits in the given value.
    static int
    count(long bits)
    Counts and returns the number of set bits in the given word.
    static int
    firstSet(long bits)
    Returns the index (0-based) of the lowest set bit (LSB-first).
    static int
    highestSet(long bits)
    Returns the index (0-based) of the highest set bit (MSB-first).
    static boolean
    isEmpty(long bits)
    Determines whether the given word contains no set bits.
    static long
    mask(long bits, int width)
    Returns the value of bits truncated to a field of width width bits (from the least significant side).
    static long
    set(long bits, long mask)
    Returns a value in which all bits specified by mask have been set to 1, leaving all other bits unchanged.
    static long
    setAt(long bits, int index)
    Sets the bit at the specified index to 1.
    static long
    toggle(long bits, long mask)
    Returns a value in which all bits specified by mask have been toggled (flipped from 0 to 1 or 1 to 0).
    static long
    toggleAt(long bits, int index)
    Toggles the bit at the specified index.
    static long
    Clears all bits and returns 0L.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • check

      public static boolean check(long bits, long mask)
      Tests whether all bits specified by mask are set to 1 in the given value.
      Parameters:
      bits - the 64-bit word to test
      mask - the mask of bits to check
      Returns:
      true if all masked bits are set, false otherwise
    • set

      public static long set(long bits, long mask)
      Returns a value in which all bits specified by mask have been set to 1, leaving all other bits unchanged.
      Parameters:
      bits - the original 64-bit value
      mask - 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 by mask have been cleared (set to 0), leaving all other bits unchanged.
      Parameters:
      bits - the original value
      mask - 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 by mask have been toggled (flipped from 0 to 1 or 1 to 0).
      Parameters:
      bits - the original value
      mask - 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 to 1.
      Parameters:
      bits - the original value
      index - the bit index to set (valid range: 0–63)
      Returns:
      the value with the selected bit set
      Throws:
      IllegalArgumentException - if index is outside 0..63
    • clearAt

      public static long clearAt(long bits, int index)
      Clears the bit at the specified index (sets it to 0).
      Parameters:
      bits - the original value
      index - 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 value
      index - 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 inspect
      index - the bit index to test (valid range: 0–63)
      Returns:
      true if the bit is set, false otherwise
    • 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 is 1.

      This method is equivalent to Long.numberOfTrailingZeros(long) when bits != 0.

      Parameters:
      bits - the 64-bit word
      Returns:
      index of the lowest set bit, or -1 if bits == 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 is 1.

      This method is equivalent to:

       63 - Long.numberOfLeadingZeros(bits)
       
      Parameters:
      bits - the 64-bit word
      Returns:
      index of the highest set bit, or -1 if bits == 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:
      true if the value is 0, false otherwise
    • unsetAll

      public static long unsetAll()
      Clears all bits and returns 0L. This is functionally equivalent to assigning 0L directly but provided for symmetry with other helpers.
      Returns:
      0L
    • mask

      public static long mask(long bits, int width)
      Returns the value of bits truncated to a field of width width bits (from the least significant side).

      If width <= 0, the method returns 0L. If width >= 64, the method returns bits unchanged.

      Internally this method constructs a low-order mask of the form (1L << width) - 1 and applies it with bitwise AND.

      Parameters:
      bits - the original value
      width - number of low bits to preserve (range 1–64)
      Returns:
      the masked value