Class Scalar

java.lang.Object
ghidra.program.model.scalar.Scalar
All Implemented Interfaces:
Comparable<Scalar>

public class Scalar extends Object implements 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

    Modifier and Type
    Method
    Description
    add(long n)
    Adds the integer n to this.
    int
    The size of this Scalar in bits.
    byte[]
    Returns a byte array representing this Scalar.
    clearBit(int n)
    The bit number n in this Scalar is set to zero.
    int
     
    boolean
     
    flipBit(int n)
    The bit number n in this Scalar is flipped.
    Returns the BigInteger representation of the value.
    long
    Get the value as signed.
    long
    Get the value as unsigned.
    long
    Returns the value as a signed value if it was created signed, otherwise the value is returned as an unsigned value
    int
     
    boolean
    Returns true if scalar was created as a signed value
    newScalar(long newValue)
    Creates a new Scalar of the same size as this scalar but with the given value
    setBit(int n)
    The bit number n in this Scalar is set to one.
    shiftLeft(int n)
    Sets this = this << n.
    shiftRight(int n)
    Sets this = this >> n using 0 as the fill bit.
    Sets this = this >> n replicating the sign bit.
    subtract(long n)
    Sets this = this - n.
    boolean
    testBit(int n)
    Returns true if and only if the designated bit is set to one.
     
    toString(int radix, boolean zeroPadded, boolean showSign, String pre, 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 Details

    • 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 Details

    • 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 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(Object obj)
      Overrides:
      equals in class Object
      See Also:
    • hashCode

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

      public int compareTo(Scalar other)
      Specified by:
      compareTo in interface Comparable<Scalar>
      See Also:
    • 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:
      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:
      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:
      IndexOutOfBoundsException - if n >= bitLength().
    • shiftLeft

      public Scalar shiftLeft(int n)

      Sets this = this << n.

      Parameters:
      n - the number of bits to shift left.
      Throws:
      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:
      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:
      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:
      IndexOutOfBoundsException - if n >= bitLength().
    • toString

      public String toString(int radix, boolean zeroPadded, boolean showSign, String pre, 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:
      IllegalArgumentException - If radix is not valid.
    • toString

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