Class BitUtil

java.lang.Object
nl.tudelft.simulation.language.util.BitUtil
All Implemented Interfaces:
Serializable

public final class BitUtil extends Object implements Serializable
Utilities for the BitSet class.

Copyright (c) 2009-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.

Author:
Peter Jacobs , Alexander Verbraeck
See Also:
  • Method Details

    • toByteArray

      public static byte[] toByteArray(BitSet bits)
      Parameters:
      bits - BitSet; the bitset to convert
      Returns:
      Returns a byte array of at least length 1. The most significant bit in the result is guaranteed not to be a 1 (since BitSet does not support sign extension). The byte-ordering of the result is big-endian which means the most significant bit is in element 0. The bit at index 0 of the bit set is assumed to be the least significant bit.
    • fromInt

      public static BitSet fromInt(int value, int length)
      returns the bitset of an integer value.
      Parameters:
      value - int; the value
      length - int; the length of the bitSet to produce
      Returns:
      the BitSet
    • fromInteger

      public static BitSet fromInteger(Integer value, int length)
      returns the bitset of an integer value.
      Parameters:
      value - Integer; the value
      length - int; the length of
      Returns:
      the BitSet
    • toInt

      public static int toInt(BitSet bits, int length)
      Parameters:
      bits - BitSet; the bitset to convert
      length - int; the length of the set
      Returns:
      Returns an int. The most significant bit in the result is guaranteed not to be a 1 (since BitSet does not support sign extension). The int-ordering of the result is big-endian which means the most significant bit is in element 0. The bit at index 0 of the bit set is assumed to be the least significant bit.
    • fromString

      public static BitSet fromString(String value)
      constructs a new BitSet from a string in the "110110" format, or the {0, 1, 3, 5, 8, 12} format. Note that for the binary representation, the LEAST SIGNIFICANT BIT COMES FIRST. So, 001 represents the value 4 and not 1.
      Parameters:
      value - String; the value
      Returns:
      the BitSet
    • fromByteArray

      public static BitSet fromByteArray(byte[] bytes)
      Parameters:
      bytes - byte[]; the byteArray
      Returns:
      Returns a bitset containing the values in bytes.The byte-ordering of bytes must be big-endian which means the most significant bit is in element 0.
    • fromBoolean

      public static BitSet fromBoolean(boolean value)
      returns a one-size BitSet with value.
      Parameters:
      value - boolean; the value of the bitSet
      Returns:
      the BitSet