Class Bits

java.lang.Object
net.emustudio.emulib.Bits

@Immutable public class Bits extends Object
Bits utility class. Supports various operations on bits. It supports maximally 32 bits, because it works with an integer.
  • Field Details

    • number

      public final int number
      The integer number in little endian.
    • length

      public final int length
      Significant bits count.
  • Constructor Details

    • Bits

      public Bits(int number, int length)
      Constructs new Bits object.
      Parameters:
      number - integer number in little endian
      length - significant bits count
  • Method Details

    • toBytes

      public byte[] toBytes()
      Converts the bits to byte array in little endian. The size of the array is always 4 bytes.
      Returns:
      byte array representing the bits in little endian
    • reverseBytes

      public Bits reverseBytes()
      Reverses the bytes.
      Returns:
      a new Bits instance with bytes reversed
    • reverseBits

      public Bits reverseBits()
      Reverses bits in each byte.

      However, order of bytes is kept.

      Returns:
      a new Bits instance with reversed bits within each byte
    • absolute

      public Bits absolute()
      Make the absolute value from the number stored in two's complement.

      If the number is already positive, it is kept as-is.

      Returns:
      a new Bits instance with the negative sign removed
    • shiftLeft

      public Bits shiftLeft()
      Shift the value to the left.
      Returns:
      a new Bits instance shifted to the left by 1 bit
    • shiftRight

      public Bits shiftRight()
      Shift the value to the right.
      Returns:
      a new Bits instance shifted to the right by 1 bit