Class Scalar
- All Implemented Interfaces:
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 -
Method Summary
Modifier and TypeMethodDescriptionadd
(long n) Adds the integer n tothis
.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
getValue()
Returns the value as a signed value if it was created signed, otherwise the value is returned as an unsigned valueint
hashCode()
boolean
isSigned()
Returns true if scalar was created as a signed valuenewScalar
(long newValue) Creates a new Scalar of the same size as this scalar but with the given valuesetBit
(int n) The bit number n in this Scalar is set to one.shiftLeft
(int n) Setsthis = this << n
.shiftRight
(int n) Setsthis = this >> n
using 0 as the fill bit.shiftRightSign
(int n) Setsthis = this >> n
replicating the sign bit.subtract
(long n) Setsthis = this - n
.boolean
testBit
(int n) Returns true if and only if the designated bit is set to one.toString()
Get a String representing this Scalar using the format defined by radix.
-
Constructor Details
-
Scalar
public Scalar(int bitLength, long value, boolean signed) Constructor- Parameters:
bitLength
- number of bitsvalue
- value of the scalarsigned
- 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 bitsvalue
- 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
Returns the BigInteger representation of the value. -
newScalar
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
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<Scalar>
- See Also:
-
add
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
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
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
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
Sets
this = this << n
.- Parameters:
n
- the number of bits to shift left.- Throws:
ArithmeticException
- if n < 0.
-
shiftRight
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
Sets
this = this >> n
replicating the sign bit.- Parameters:
n
- the number of bits to arithmetically shift.- Throws:
ArithmeticException
- if n < 0.
-
subtract
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
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
-