Class LargeIntHandler

java.lang.Object
com.pnfsoftware.jeb.util.primitives.LargeIntHandler

public class LargeIntHandler extends Object
Handler for large integers of a given maximum bitsize. Those objects are created through a factory method.
  • Method Details

    • create

      public static LargeIntHandler create(int bitsize)
      Create or retrieve a handler for a fixed integer bit size.
      Parameters:
      bitsize - integer bit size
      Returns:
      a cached handler for the bit size
    • getBitsize

      public int getBitsize()
      Get the fixed integer bit size.
      Returns:
      the bit size
    • create

      public BigInteger create(String val)
      Create a new big integer. The client is responsible for making sure that the two-complement representation of the value fits within bitsize bits.
      Parameters:
      val - string value (decimal, hex, or octal - with the right associated prefix or suffix)
      Returns:
      the parsed value
    • truncate

      public BigInteger truncate(String val)
      Truncate and store as a signed value. The resulting integer 2-complement representation holds on at most bitsize bits.
      Parameters:
      val - string value (decimal, hex, or octal - with the right associated prefix or suffix)
      Returns:
      the truncated signed value
    • truncate

      public BigInteger truncate(BigInteger v)
      Truncate and store as a signed value. The resulting integer 2-complement representation holds on at most bitsize bits.
      Parameters:
      v - value to truncate
      Returns:
      the truncated signed value
    • toUnsigned

      public BigInteger toUnsigned(BigInteger a)
      Get the unsigned equivalent of the provided truncated integer.

      Warning, this method is unsafe! The result may no longer be representable on bitsize bits.

      Parameters:
      a - signed value
      Returns:
      the unsigned equivalent
    • neg

      public BigInteger neg(BigInteger a)
      Negate a signed value and truncate the result.
      Parameters:
      a - value to negate
      Returns:
      the truncated result
    • add

      public BigInteger add(BigInteger a, BigInteger b)
      Add two signed values and truncate the result.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • sub

      public BigInteger sub(BigInteger a, BigInteger b)
      Subtract two signed values and truncate the result.
      Parameters:
      a - minuend
      b - subtrahend
      Returns:
      the truncated result
    • mulS

      public BigInteger mulS(BigInteger a, BigInteger b)
      Multiply two signed values and truncate the result to the handler bit size.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • mulU

      public BigInteger mulU(BigInteger a, BigInteger b)
      Multiply two unsigned values and truncate the result to the handler bit size.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • mul2S

      public BigInteger mul2S(BigInteger a, BigInteger b)
      Multiply two signed values and truncate the result to twice the handler bit size.
      Parameters:
      a - first value
      b - second value
      Returns:
      the double-width truncated result
    • mul2U

      public BigInteger mul2U(BigInteger a, BigInteger b)
      Multiply two unsigned values and truncate the result to twice the handler bit size.
      Parameters:
      a - first value
      b - second value
      Returns:
      the double-width truncated result
    • divS

      public BigInteger divS(BigInteger a, BigInteger b)
      Divide two signed values and truncate the quotient.
      Parameters:
      a - dividend
      b - divisor
      Returns:
      the truncated quotient
    • divU

      public BigInteger divU(BigInteger a, BigInteger b)
      Divide two unsigned values and truncate the quotient.
      Parameters:
      a - dividend
      b - divisor
      Returns:
      the truncated quotient
    • remS

      public BigInteger remS(BigInteger a, BigInteger b)
      Compute the signed remainder and truncate it.
      Parameters:
      a - dividend
      b - divisor
      Returns:
      the truncated remainder
    • remU

      public BigInteger remU(BigInteger a, BigInteger b)
      Compute the unsigned remainder and truncate it.
      Parameters:
      a - dividend
      b - divisor
      Returns:
      the truncated remainder
    • div2S

      public BigInteger[] div2S(BigInteger a, BigInteger b)
      Divide a double-width signed dividend by a signed divisor.
      Parameters:
      a - double-width dividend
      b - divisor
      Returns:
      two elements: truncated quotient and truncated remainder
    • div2U

      public BigInteger[] div2U(BigInteger a, BigInteger b)
      Divide a double-width unsigned dividend by an unsigned divisor.
      Parameters:
      a - double-width dividend
      b - divisor
      Returns:
      two elements: truncated quotient and truncated remainder
    • and

      public BigInteger and(BigInteger a, BigInteger b)
      Bitwise AND two values and truncate the result.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • or

      public BigInteger or(BigInteger a, BigInteger b)
      Bitwise OR two values and truncate the result.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • not

      public BigInteger not(BigInteger a)
      Bitwise NOT a value and truncate the result.
      Parameters:
      a - value
      Returns:
      the truncated result
    • xor

      public BigInteger xor(BigInteger a, BigInteger b)
      Bitwise XOR two values and truncate the result.
      Parameters:
      a - first value
      b - second value
      Returns:
      the truncated result
    • testbit

      public boolean testbit(BigInteger a, int n)
      Test a bit in a value.
      Parameters:
      a - value
      n - bit index
      Returns:
      true if the bit is set
    • setbit

      public BigInteger setbit(BigInteger a, int n)
      Set a bit in a value and truncate the result.
      Parameters:
      a - value
      n - bit index
      Returns:
      the truncated result
    • clearbit

      public BigInteger clearbit(BigInteger a, int n)
      Clear a bit in a value and truncate the result.
      Parameters:
      a - value
      n - bit index
      Returns:
      the truncated result
    • shl

      public BigInteger shl(BigInteger a, int n)
      Shift left with a modulo-count.
      Parameters:
      a - value to shift
      n - shift count
      Returns:
      the truncated result
    • shr

      public BigInteger shr(BigInteger a, int n)
      Logical shift right with a modulo-count.
      Parameters:
      a - value to shift
      n - shift count
      Returns:
      the truncated result
    • sar

      public BigInteger sar(BigInteger a, int n)
      Arithmetic shift right with a modulo-count.
      Parameters:
      a - value to shift
      n - shift count
      Returns:
      the truncated result
    • ror

      public BigInteger ror(BigInteger a, int n)
      Rotate right with a modulo-count.
      Parameters:
      a - value to rotate
      n - rotation count
      Returns:
      the truncated result
    • rol

      public BigInteger rol(BigInteger a, int n)
      Rotate left with a modulo-count.
      Parameters:
      a - value to rotate
      n - rotation count
      Returns:
      the truncated result
    • compare

      public int compare(BigInteger a, BigInteger b)
      Compare two signed values.
      Parameters:
      a - first value
      b - second value
      Returns:
      a negative value, zero, or a positive value if a is respectively below, equal to, or above b
    • compareU

      public int compareU(BigInteger a, BigInteger b)
      Compare two unsigned values.
      Parameters:
      a - first value
      b - second value
      Returns:
      a negative value, zero, or a positive value if a is respectively below, equal to, or above b in unsigned order
    • toUnsignedHexString

      public String toUnsignedHexString(BigInteger v)
      Format a value as an unsigned hexadecimal string.
      Parameters:
      v - value to format
      Returns:
      the upper-case unsigned hexadecimal representation