Class Scalar

  • All Implemented Interfaces:
    java.lang.Comparable<Scalar>

    public class Scalar
    extends java.lang.Object
    implements java.lang.Comparable<Scalar>

    The Scalar defines a immutable fixed bit signed integer. Bit operations on a Scalar expect Scalar to act as a number in the two's complement format. Scalar was designed to be used as an offset (difference between two Addresses), an arithmetic operand, and also potentially for simulating registers.

    If an operation varies depending on whether the Scalar is treated as signed or unsigned, there are usally two version such as multiply and unsignedMultiply. Please note that this means that the Comparable interface treats the number as signed.

    • Constructor Summary

      Constructors 
      Constructor Description
      Scalar​(int bitLength, long value)
      Constructor a new signed scalar object.
      Scalar​(int bitLength, long value, boolean signed)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Scalar add​(long n)
      Adds the integer n to this.
      int bitLength()
      The size of this Scalar in bits.
      byte[] byteArrayValue()
      Returns a byte array representing this Scalar.
      Scalar clearBit​(int n)
      The bit number n in this Scalar is set to zero.
      int compareTo​(Scalar other)  
      boolean equals​(java.lang.Object obj)  
      Scalar flipBit​(int n)
      The bit number n in this Scalar is flipped.
      java.math.BigInteger getBigInteger()
      Returns the BigInteger representation of the value.
      long getSignedValue()
      Get the value as signed.
      long getUnsignedValue()
      Get the value as unsigned.
      long getValue()
      Returns the value as a signed value if it was created signed, otherwise the value is returned as an unsigned value
      int hashCode()  
      boolean isSigned()
      Returns true if scalar was created as a signed value
      Scalar newScalar​(long newValue)
      Creates a new Scalar of the same size as this scalar but with the given value
      Scalar setBit​(int n)
      The bit number n in this Scalar is set to one.
      Scalar shiftLeft​(int n)
      Sets this = this << n.
      Scalar shiftRight​(int n)
      Sets this = this >> n using 0 as the fill bit.
      Scalar shiftRightSign​(int n)
      Sets this = this >> n replicating the sign bit.
      Scalar subtract​(long n)
      Sets this = this - n.
      boolean testBit​(int n)
      Returns true if and only if the designated bit is set to one.
      java.lang.String toString()  
      java.lang.String toString​(int radix, boolean zeroPadded, boolean showSign, java.lang.String pre, java.lang.String post)
      Get a String representing this Scalar using the format defined by radix.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Scalar

        public Scalar​(int bitLength,
                      long value,
                      boolean signed)
        Constructor
        Parameters:
        bitLength - number of bits
        value - value of the scalar
        signed - true for a signed value, false for an unsigned value.
      • Scalar

        public Scalar​(int bitLength,
                      long value)
        Constructor a new signed scalar object.
        Parameters:
        bitLength - number of bits
        value - value of the scalar
    • Method Detail

      • isSigned

        public boolean isSigned()
        Returns true if scalar was created as a signed value
      • getSignedValue

        public long getSignedValue()
        Get the value as signed.
      • getUnsignedValue

        public long getUnsignedValue()
        Get the value as unsigned.
      • getValue

        public long getValue()
        Returns the value as a signed value if it was created signed, otherwise the value is returned as an unsigned value
      • getBigInteger

        public java.math.BigInteger getBigInteger()
        Returns the BigInteger representation of the value.
      • newScalar

        public Scalar newScalar​(long newValue)

        Creates a new Scalar of the same size as this scalar but with the given value

        Parameters:
        newValue - the Scalar value which will be used to initialize the new Scalar.
        Returns:
        the new Scalar.
      • byteArrayValue

        public byte[] byteArrayValue()

        Returns a byte array representing this Scalar. The size of the byte array is the number of bytes required to hold the number of bits returned by bitLength().

        Returns:
        a big-endian byte array containing the bits in this Scalar.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • compareTo

        public int compareTo​(Scalar other)
        Specified by:
        compareTo in interface java.lang.Comparable<Scalar>
        See Also:
        Comparable.compareTo(java.lang.Object)
      • add

        public Scalar add​(long n)

        Adds the integer n to this. Computes (this = this + n).

        Parameters:
        n - the value to add to this scalars value to produce a new scalar.
      • bitLength

        public int bitLength()

        The size of this Scalar in bits. This is constant for a Scalar. It is not dependent on the particular value of the scalar. For example, a 16-bit Scalar should always return 16 regardless of the actual value held.

        Returns:
        the width of this Scalar.
      • clearBit

        public Scalar clearBit​(int n)

        The bit number n in this Scalar is set to zero. Computes (this = this & ~(1<<n)). Bits are numbered 0..bitlength()-1 with 0 being the least significant bit.

        Parameters:
        n - the bit to clear in this scalar.
        Throws:
        java.lang.IndexOutOfBoundsException - if n >= bitLength().
      • flipBit

        public Scalar flipBit​(int n)

        The bit number n in this Scalar is flipped. Computes (this = this ^ (1<<n)). Bits are numbered 0..bitlength()-1 with 0 being the least significant bit.

        Parameters:
        n - the bit to flip.
        Throws:
        java.lang.IndexOutOfBoundsException - if n >= bitLength().
      • setBit

        public Scalar setBit​(int n)

        The bit number n in this Scalar is set to one. Computes (this = this | (1<<n)). Bits are numbered 0..bitlength()-1 with 0 being the least significant bit.

        Parameters:
        n - the bit to set.
        Throws:
        java.lang.IndexOutOfBoundsException - if n >= bitLength().
      • shiftLeft

        public Scalar shiftLeft​(int n)

        Sets this = this << n.

        Parameters:
        n - the number of bits to shift left.
        Throws:
        java.lang.ArithmeticException - if n < 0.
      • shiftRight

        public Scalar shiftRight​(int n)

        Sets this = this >> n using 0 as the fill bit.

        Parameters:
        n - the number of bits to shift right.
        Throws:
        java.lang.ArithmeticException - if n < 0.
      • shiftRightSign

        public Scalar shiftRightSign​(int n)

        Sets this = this >> n replicating the sign bit.

        Parameters:
        n - the number of bits to arithmetically shift.
        Throws:
        java.lang.ArithmeticException - if n < 0.
      • subtract

        public Scalar subtract​(long n)

        Sets this = this - n.

        Parameters:
        n - the value to subtract from this scalar to produce a new scalar.
      • testBit

        public boolean testBit​(int n)

        Returns true if and only if the designated bit is set to one. Computes ((this & (1<<n)) != 0). Bits are numbered 0..bitlength()-1 with 0 being the least significant bit.

        Parameters:
        n - the bit to test.
        Returns:
        true if and only if the designated bit is set to one.
        Throws:
        java.lang.IndexOutOfBoundsException - if n >= bitLength().
      • toString

        public java.lang.String toString​(int radix,
                                         boolean zeroPadded,
                                         boolean showSign,
                                         java.lang.String pre,
                                         java.lang.String post)

        Get a String representing this Scalar using the format defined by radix.

        Parameters:
        radix - an integer base to use in representing the number (only 2, 8, 10, 16 are valid). If 10 is specified, all remaining parameters are ignored.
        zeroPadded - a boolean which if true will have the number left padded with 0 to the width necessary to hold the maximum value.
        showSign - if true the '-' sign will be prepended for negative values, else value will be treated as an unsigned value and output without a sign.
        pre - a String to append after the sign (if signed) but before the digits.
        post - a String to append after the digits.
        Returns:
        a String representation of this scalar.
        Throws:
        java.lang.IllegalArgumentException - If radix is not valid.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()