Class UInt256

java.lang.Object
org.apache.tuweni.units.bigints.UInt256
All Implemented Interfaces:
Comparable<org.apache.tuweni.bytes.Bytes>, org.apache.tuweni.bytes.Bytes, org.apache.tuweni.bytes.Bytes32, UInt256Value<UInt256>

public final class UInt256 extends Object implements UInt256Value<UInt256>
An unsigned 256-bit precision number.

This is a raw UInt256Value - a 256-bit precision unsigned number of no particular unit.

  • Field Details

    • MIN_VALUE

      public static final UInt256 MIN_VALUE
      The minimum value of a UInt256
    • MAX_VALUE

      public static final UInt256 MAX_VALUE
      The maximum value of a UInt256
    • ZERO

      public static final UInt256 ZERO
      The value 0
    • ONE

      public static final UInt256 ONE
      The value 1
  • Method Details

    • valueOf

      public static UInt256 valueOf(long value)
      Return a UInt256 containing the specified value.
      Parameters:
      value - The value to create a UInt256 for.
      Returns:
      A UInt256 containing the specified value.
      Throws:
      IllegalArgumentException - If the value is negative.
    • valueOf

      public static UInt256 valueOf(BigInteger value)
      Return a UInt256 containing the specified value.
      Parameters:
      value - the value to create a UInt256 for
      Returns:
      a UInt256 containing the specified value
      Throws:
      IllegalArgumentException - if the value is negative or too large to be represented as a UInt256
    • fromBytes

      public static UInt256 fromBytes(org.apache.tuweni.bytes.Bytes bytes)
      Return a UInt256 containing the value described by the specified bytes.
      Parameters:
      bytes - The bytes containing a UInt256.
      Returns:
      A UInt256 containing the specified value.
      Throws:
      IllegalArgumentException - if bytes.size() > 32.
    • fromHexString

      public static UInt256 fromHexString(String str)
      Parse a hexadecimal string into a UInt256.
      Parameters:
      str - The hexadecimal string to parse, which may or may not start with "0x". That representation may contain less than 32 bytes, in which case the result is left padded with zeros.
      Returns:
      The value corresponding to str.
      Throws:
      IllegalArgumentException - if str does not correspond to a valid hexadecimal representation or contains more than 32 bytes.
    • isZero

      public boolean isZero()
      Specified by:
      isZero in interface org.apache.tuweni.bytes.Bytes
    • add

      public UInt256 add(UInt256 value)
      Description copied from interface: UInt256Value
      Returns a value that is (this + value).
      Specified by:
      add in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be added to this value.
      Returns:
      this + value
    • add

      public UInt256 add(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this + value).
      Specified by:
      add in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be added to this value.
      Returns:
      this + value
    • addMod

      public UInt256 addMod(UInt256 value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • addMod

      public UInt256 addMod(long value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • addMod

      public UInt256 addMod(long value, long modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • subtract

      public UInt256 subtract(UInt256 value)
      Description copied from interface: UInt256Value
      Returns a value that is (this - value).
      Specified by:
      subtract in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be subtracted from this value.
      Returns:
      this - value
    • subtract

      public UInt256 subtract(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this - value).
      Specified by:
      subtract in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to be subtracted from this value.
      Returns:
      this - value
    • multiply

      public UInt256 multiply(UInt256 value)
      Description copied from interface: UInt256Value
      Returns a value that is (this * value).
      Specified by:
      multiply in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to multiply this value by.
      Returns:
      this * value
    • multiply

      public UInt256 multiply(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this * value).
      Specified by:
      multiply in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to multiply this value by.
      Returns:
      this * value
    • multiplyMod

      public UInt256 multiplyMod(UInt256 value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • multiplyMod

      public UInt256 multiplyMod(long value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • multiplyMod

      public UInt256 multiplyMod(long value, long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • divide

      public UInt256 divide(UInt256 value)
      Description copied from interface: UInt256Value
      Returns a value that is (this / value).
      Specified by:
      divide in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
    • divide

      public UInt256 divide(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this / value).
      Specified by:
      divide in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
    • sdiv0

      public UInt256 sdiv0(UInt256 divisor)
    • divideCeil

      public UInt256 divideCeil(UInt256 value)
      Description copied from interface: UInt256Value
      Returns a value that is ceiling(this / value).
      Specified by:
      divideCeil in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value + ( this % value == 0 ? 0 : 1)
    • divideCeil

      public UInt256 divideCeil(long value)
      Description copied from interface: UInt256Value
      Returns a value that is ceiling(this / value).
      Specified by:
      divideCeil in interface UInt256Value<UInt256>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value + ( this % value == 0 ? 0 : 1)
    • pow

      public UInt256 pow(UInt256 exponent)
      Description copied from interface: UInt256Value
      Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)

      This calculates an exponentiation over the modulus of 2^256.

      Note that exponent is an UInt256 rather than of the type T.

      Specified by:
      pow in interface UInt256Value<UInt256>
      Parameters:
      exponent - The exponent to which this value is to be raised.
      Returns:
      this<sup>exponent</sup> mod 2<sup>256</sup>
    • pow

      public UInt256 pow(long exponent)
      Description copied from interface: UInt256Value
      Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)

      This calculates an exponentiation over the modulus of 2^256.

      Specified by:
      pow in interface UInt256Value<UInt256>
      Parameters:
      exponent - The exponent to which this value is to be raised.
      Returns:
      this<sup>exponent</sup> mod 2<sup>256</sup>
    • mod

      public UInt256 mod(UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus).
      Specified by:
      mod in interface UInt256Value<UInt256>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • mod

      public UInt256 mod(long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus).
      Specified by:
      mod in interface UInt256Value<UInt256>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • mod0

      public UInt256 mod0(UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus), or 0 if modulus is 0.
      Specified by:
      mod0 in interface UInt256Value<UInt256>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • smod0

      public UInt256 smod0(UInt256 modulus)
      Returns a value that is the (this signed mod modulus), or 0 if modulus is 0.
      Parameters:
      modulus - The modulus.
      Returns:
      this signed mod modulus.
    • mod0

      public UInt256 mod0(long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus), or 0 if modulus is 0.
      Specified by:
      mod0 in interface UInt256Value<UInt256>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • and

      public UInt256 and(UInt256 value)
      Return a bit-wise AND of this value and the supplied value.
      Parameters:
      value - the value to perform the operation with
      Returns:
      the result of a bit-wise AND
    • and

      public UInt256 and(org.apache.tuweni.bytes.Bytes32 bytes)
      Return a bit-wise AND of this value and the supplied bytes.
      Specified by:
      and in interface org.apache.tuweni.bytes.Bytes32
      Parameters:
      bytes - the bytes to perform the operation with
      Returns:
      the result of a bit-wise AND
    • or

      public UInt256 or(UInt256 value)
      Return a bit-wise OR of this value and the supplied value.
      Parameters:
      value - the value to perform the operation with
      Returns:
      the result of a bit-wise OR
    • or

      public UInt256 or(org.apache.tuweni.bytes.Bytes32 bytes)
      Return a bit-wise OR of this value and the supplied bytes.
      Specified by:
      or in interface org.apache.tuweni.bytes.Bytes32
      Parameters:
      bytes - the bytes to perform the operation with
      Returns:
      the result of a bit-wise OR
    • xor

      public UInt256 xor(UInt256 value)
      Return a bit-wise XOR of this value and the supplied value.
      Parameters:
      value - the value to perform the operation with
      Returns:
      the result of a bit-wise XOR
    • xor

      public UInt256 xor(org.apache.tuweni.bytes.Bytes32 bytes)
      Return a bit-wise XOR of this value and the supplied bytes.
      Specified by:
      xor in interface org.apache.tuweni.bytes.Bytes32
      Parameters:
      bytes - the bytes to perform the operation with
      Returns:
      the result of a bit-wise XOR
    • not

      public UInt256 not()
      Return a bit-wise NOT of this value.
      Specified by:
      not in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      not in interface org.apache.tuweni.bytes.Bytes32
      Returns:
      the result of a bit-wise NOT
    • shiftRight

      public UInt256 shiftRight(int distance)
      Shift all bits in this value to the right.
      Specified by:
      shiftRight in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      shiftRight in interface org.apache.tuweni.bytes.Bytes32
      Parameters:
      distance - The number of bits to shift by.
      Returns:
      A value containing the shifted bits.
    • shiftLeft

      public UInt256 shiftLeft(int distance)
      Shift all bits in this value to the left.
      Specified by:
      shiftLeft in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      shiftLeft in interface org.apache.tuweni.bytes.Bytes32
      Parameters:
      distance - The number of bits to shift by.
      Returns:
      A value containing the shifted bits.
    • equals

      public boolean equals(@Nullable @Nullable Object object)
      Overrides:
      equals in class Object
    • hashCode

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

      public boolean fitsInt()
      Description copied from interface: UInt256Value
      Returns true if the value can fit in an int.
      Specified by:
      fitsInt in interface UInt256Value<UInt256>
      Returns:
      True if this value fits a java int (i.e. is less or equal to Integer.MAX_VALUE).
    • intValue

      public int intValue()
      Description copied from interface: UInt256Value
      Provides this value as an int.
      Specified by:
      intValue in interface UInt256Value<UInt256>
      Returns:
      This value as a java int assuming it is small enough to fit an int.
    • fitsLong

      public boolean fitsLong()
      Description copied from interface: UInt256Value
      Returns true if the value can fit in a long.
      Specified by:
      fitsLong in interface UInt256Value<UInt256>
      Returns:
      True if this value fits a java long (i.e. is less or equal to Long.MAX_VALUE).
    • get

      public byte get(int i)
      Specified by:
      get in interface org.apache.tuweni.bytes.Bytes
    • copy

      public org.apache.tuweni.bytes.Bytes32 copy()
      Specified by:
      copy in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      copy in interface org.apache.tuweni.bytes.Bytes32
    • toLong

      public long toLong()
      Specified by:
      toLong in interface org.apache.tuweni.bytes.Bytes
    • toString

      public String toString()
      Specified by:
      toString in interface org.apache.tuweni.bytes.Bytes
      Overrides:
      toString in class Object
    • toUInt256

      public UInt256 toUInt256()
      Description copied from interface: UInt256Value
      Convert this value to a UInt256.
      Specified by:
      toUInt256 in interface UInt256Value<UInt256>
      Returns:
      This value as a UInt256.
    • slice

      public org.apache.tuweni.bytes.Bytes slice(int i, int length)
      Specified by:
      slice in interface org.apache.tuweni.bytes.Bytes
    • mutableCopy

      public org.apache.tuweni.bytes.MutableBytes32 mutableCopy()
      Specified by:
      mutableCopy in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      mutableCopy in interface org.apache.tuweni.bytes.Bytes32
    • toBytes

      public org.apache.tuweni.bytes.Bytes32 toBytes()
      Description copied from interface: UInt256Value
      Provides the value as bytes.
      Specified by:
      toBytes in interface UInt256Value<UInt256>
      Returns:
      The value as bytes.
    • toMinimalBytes

      public org.apache.tuweni.bytes.Bytes toMinimalBytes()
      Description copied from interface: UInt256Value
      Provides the value as bytes without any leading zero bytes.
      Specified by:
      toMinimalBytes in interface UInt256Value<UInt256>
      Returns:
      The value as bytes without any leading zero bytes.
    • numberOfLeadingZeros

      public int numberOfLeadingZeros()
      Specified by:
      numberOfLeadingZeros in interface org.apache.tuweni.bytes.Bytes
    • bitLength

      public int bitLength()
      Specified by:
      bitLength in interface org.apache.tuweni.bytes.Bytes
    • max

      public UInt256 max()
      Description copied from interface: UInt256Value
      Return the max value for this type.

      The default implementation of this method returns a value obtained from calling the concrete type constructor with an argument of MAX_VALUE. Most implementations will want to override this method to instead return a static constant.

      Specified by:
      max in interface UInt256Value<UInt256>
      Returns:
      The max value for this type.