Package org.apache.tuweni.units.bigints
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>
An unsigned 256-bit precision number.
This is a raw UInt256Value - a 256-bit precision unsigned number of no particular
unit.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final UInt256The maximum value of a UInt256static final UInt256The minimum value of a UInt256static final UInt256The value 1static final UInt256The value 0Fields inherited from interface org.apache.tuweni.bytes.Bytes
EMPTYFields inherited from interface org.apache.tuweni.bytes.Bytes32
SIZE -
Method Summary
Modifier and TypeMethodDescriptionadd(long value) Returns a value that is(this + value).Returns a value that is(this + value).addMod(long value, long modulus) Returns a value equivalent to((this + value) mod modulus).Returns a value equivalent to((this + value) mod modulus).Returns a value equivalent to((this + value) mod modulus).and(org.apache.tuweni.bytes.Bytes32 bytes) Return a bit-wise AND of this value and the supplied bytes.Return a bit-wise AND of this value and the supplied value.intorg.apache.tuweni.bytes.Bytes32copy()divide(long value) Returns a value that is(this / value).Returns a value that is(this / value).divideCeil(long value) Returns a value that isceiling(this / value).divideCeil(UInt256 value) Returns a value that isceiling(this / value).booleanbooleanfitsInt()Returns true if the value can fit in an int.booleanfitsLong()Returns true if the value can fit in a long.static UInt256fromBytes(org.apache.tuweni.bytes.Bytes bytes) Return aUInt256containing the value described by the specified bytes.static UInt256fromHexString(String str) Parse a hexadecimal string into aUInt256.byteget(int i) inthashCode()intintValue()Provides this value as an int.booleanisZero()max()Return the max value for this type.mod(long modulus) Returns a value that is(this mod modulus).Returns a value that is(this mod modulus).mod0(long modulus) Returns a value that is(this mod modulus), or 0 if modulus is 0.Returns a value that is(this mod modulus), or 0 if modulus is 0.multiply(long value) Returns a value that is(this * value).Returns a value that is(this * value).multiplyMod(long value, long modulus) Returns a value that is((this * value) mod modulus).multiplyMod(long value, UInt256 modulus) Returns a value that is((this * value) mod modulus).multiplyMod(UInt256 value, UInt256 modulus) Returns a value that is((this * value) mod modulus).org.apache.tuweni.bytes.MutableBytes32not()Return a bit-wise NOT of this value.intor(org.apache.tuweni.bytes.Bytes32 bytes) Return a bit-wise OR of this value and the supplied bytes.Return a bit-wise OR of this value and the supplied value.pow(long exponent) Returns a value that is(this<sup>exponent</sup> mod 2<sup>256</sup>)Returns a value that is(this<sup>exponent</sup> mod 2<sup>256</sup>)shiftLeft(int distance) Shift all bits in this value to the left.shiftRight(int distance) Shift all bits in this value to the right.org.apache.tuweni.bytes.Bytesslice(int i, int length) Returns a value that is the(this signed mod modulus), or 0 if modulus is 0.subtract(long value) Returns a value that is(this - value).Returns a value that is(this - value).org.apache.tuweni.bytes.Bytes32toBytes()Provides the value as bytes.longtoLong()org.apache.tuweni.bytes.BytesProvides the value as bytes without any leading zero bytes.toString()Convert this value to aUInt256.static UInt256valueOf(long value) Return aUInt256containing the specified value.static UInt256valueOf(BigInteger value) Return aUInt256containing the specified value.xor(org.apache.tuweni.bytes.Bytes32 bytes) Return a bit-wise XOR of this value and the supplied bytes.Return a bit-wise XOR of this value and the supplied value.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.tuweni.bytes.Bytes
and, and, appendHexTo, appendTo, appendTo, commonPrefix, commonPrefixLength, compareTo, copyTo, copyTo, getInt, getInt, getLong, getLong, hasLeadingZero, hasLeadingZeroByte, isEmpty, not, numberOfLeadingZeroBytes, numberOfTrailingZeroBytes, or, or, reverse, shiftLeft, shiftRight, slice, toArray, toArray, toArrayUnsafe, toBase64String, toEllipsisHexString, toFastHex, toHexString, toInt, toQuantityHexString, toShortHexString, toUnprefixedHexString, toUnsignedBigInteger, toUnsignedBigInteger, trimLeadingZeros, trimTrailingZeros, update, xor, xorMethods inherited from interface org.apache.tuweni.bytes.Bytes32
sizeMethods inherited from interface org.apache.tuweni.units.bigints.UInt256Value
addExact, addExact, addSafe, addSafe, fitsInt, fitsLong, greaterOrEqualThan, greaterThan, lessOrEqualThan, lessThan, plus, plus, subtractExact, subtractExact, toBigInteger, toBigInteger, toDecimalString, toInt, toLong, toSignedBigInteger, toSignedBigInteger
-
Field Details
-
MIN_VALUE
The minimum value of a UInt256 -
MAX_VALUE
The maximum value of a UInt256 -
ZERO
The value 0 -
ONE
The value 1
-
-
Method Details
-
valueOf
Return aUInt256containing the specified value.- Parameters:
value- The value to create aUInt256for.- Returns:
- A
UInt256containing the specified value. - Throws:
IllegalArgumentException- If the value is negative.
-
valueOf
Return aUInt256containing the specified value.- Parameters:
value- the value to create aUInt256for- Returns:
- a
UInt256containing the specified value - Throws:
IllegalArgumentException- if the value is negative or too large to be represented as a UInt256
-
fromBytes
Return aUInt256containing the value described by the specified bytes.- Parameters:
bytes- The bytes containing aUInt256.- Returns:
- A
UInt256containing the specified value. - Throws:
IllegalArgumentException- ifbytes.size() > 32.
-
fromHexString
Parse a hexadecimal string into aUInt256.- 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- ifstrdoes not correspond to a valid hexadecimal representation or contains more than 32 bytes.
-
isZero
public boolean isZero()- Specified by:
isZeroin interfaceorg.apache.tuweni.bytes.Bytes
-
add
Description copied from interface:UInt256ValueReturns a value that is(this + value).- Specified by:
addin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
add
Description copied from interface:UInt256ValueReturns a value that is(this + value).- Specified by:
addin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
addMod
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
addMod
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
addMod
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
subtract
Description copied from interface:UInt256ValueReturns a value that is(this - value).- Specified by:
subtractin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
subtract
Description copied from interface:UInt256ValueReturns a value that is(this - value).- Specified by:
subtractin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
multiply
Description copied from interface:UInt256ValueReturns a value that is(this * value).- Specified by:
multiplyin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to multiply this value by.- Returns:
this * value
-
multiply
Description copied from interface:UInt256ValueReturns a value that is(this * value).- Specified by:
multiplyin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to multiply this value by.- Returns:
this * value
-
multiplyMod
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
multiplyMod
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
multiplyMod
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
divide
Description copied from interface:UInt256ValueReturns a value that is(this / value).- Specified by:
dividein interfaceUInt256Value<UInt256>- Parameters:
value- The amount to divide this value by.- Returns:
this / value
-
divide
Description copied from interface:UInt256ValueReturns a value that is(this / value).- Specified by:
dividein interfaceUInt256Value<UInt256>- Parameters:
value- The amount to divide this value by.- Returns:
this / value
-
sdiv0
-
divideCeil
Description copied from interface:UInt256ValueReturns a value that isceiling(this / value).- Specified by:
divideCeilin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)
-
divideCeil
Description copied from interface:UInt256ValueReturns a value that isceiling(this / value).- Specified by:
divideCeilin interfaceUInt256Value<UInt256>- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)
-
pow
Description copied from interface:UInt256ValueReturns 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
exponentis anUInt256rather than of the typeT.- Specified by:
powin interfaceUInt256Value<UInt256>- Parameters:
exponent- The exponent to which this value is to be raised.- Returns:
this<sup>exponent</sup> mod 2<sup>256</sup>
-
pow
Description copied from interface:UInt256ValueReturns 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:
powin interfaceUInt256Value<UInt256>- Parameters:
exponent- The exponent to which this value is to be raised.- Returns:
this<sup>exponent</sup> mod 2<sup>256</sup>
-
mod
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus).- Specified by:
modin interfaceUInt256Value<UInt256>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus).- Specified by:
modin interfaceUInt256Value<UInt256>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod0
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus), or 0 if modulus is 0.- Specified by:
mod0in interfaceUInt256Value<UInt256>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
smod0
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
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus), or 0 if modulus is 0.- Specified by:
mod0in interfaceUInt256Value<UInt256>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
and
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
Return a bit-wise AND of this value and the supplied bytes.- Specified by:
andin interfaceorg.apache.tuweni.bytes.Bytes32- Parameters:
bytes- the bytes to perform the operation with- Returns:
- the result of a bit-wise AND
-
or
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
Return a bit-wise OR of this value and the supplied bytes.- Specified by:
orin interfaceorg.apache.tuweni.bytes.Bytes32- Parameters:
bytes- the bytes to perform the operation with- Returns:
- the result of a bit-wise OR
-
xor
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
Return a bit-wise XOR of this value and the supplied bytes.- Specified by:
xorin interfaceorg.apache.tuweni.bytes.Bytes32- Parameters:
bytes- the bytes to perform the operation with- Returns:
- the result of a bit-wise XOR
-
not
Return a bit-wise NOT of this value.- Specified by:
notin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
notin interfaceorg.apache.tuweni.bytes.Bytes32- Returns:
- the result of a bit-wise NOT
-
shiftRight
Shift all bits in this value to the right.- Specified by:
shiftRightin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
shiftRightin interfaceorg.apache.tuweni.bytes.Bytes32- Parameters:
distance- The number of bits to shift by.- Returns:
- A value containing the shifted bits.
-
shiftLeft
Shift all bits in this value to the left.- Specified by:
shiftLeftin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
shiftLeftin interfaceorg.apache.tuweni.bytes.Bytes32- Parameters:
distance- The number of bits to shift by.- Returns:
- A value containing the shifted bits.
-
equals
-
hashCode
public int hashCode() -
fitsInt
public boolean fitsInt()Description copied from interface:UInt256ValueReturns true if the value can fit in an int.- Specified by:
fitsIntin interfaceUInt256Value<UInt256>- Returns:
- True if this value fits a java
int(i.e. is less or equal toInteger.MAX_VALUE).
-
intValue
public int intValue()Description copied from interface:UInt256ValueProvides this value as an int.- Specified by:
intValuein interfaceUInt256Value<UInt256>- Returns:
- This value as a java
intassuming it is small enough to fit anint.
-
fitsLong
public boolean fitsLong()Description copied from interface:UInt256ValueReturns true if the value can fit in a long.- Specified by:
fitsLongin interfaceUInt256Value<UInt256>- Returns:
- True if this value fits a java
long(i.e. is less or equal toLong.MAX_VALUE).
-
get
public byte get(int i) - Specified by:
getin interfaceorg.apache.tuweni.bytes.Bytes
-
copy
public org.apache.tuweni.bytes.Bytes32 copy()- Specified by:
copyin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
copyin interfaceorg.apache.tuweni.bytes.Bytes32
-
toLong
public long toLong()- Specified by:
toLongin interfaceorg.apache.tuweni.bytes.Bytes
-
toString
-
toUInt256
Description copied from interface:UInt256ValueConvert this value to aUInt256.- Specified by:
toUInt256in interfaceUInt256Value<UInt256>- Returns:
- This value as a
UInt256.
-
slice
public org.apache.tuweni.bytes.Bytes slice(int i, int length) - Specified by:
slicein interfaceorg.apache.tuweni.bytes.Bytes
-
mutableCopy
public org.apache.tuweni.bytes.MutableBytes32 mutableCopy()- Specified by:
mutableCopyin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
mutableCopyin interfaceorg.apache.tuweni.bytes.Bytes32
-
toBytes
public org.apache.tuweni.bytes.Bytes32 toBytes()Description copied from interface:UInt256ValueProvides the value as bytes.- Specified by:
toBytesin interfaceUInt256Value<UInt256>- Returns:
- The value as bytes.
-
toMinimalBytes
public org.apache.tuweni.bytes.Bytes toMinimalBytes()Description copied from interface:UInt256ValueProvides the value as bytes without any leading zero bytes.- Specified by:
toMinimalBytesin interfaceUInt256Value<UInt256>- Returns:
- The value as bytes without any leading zero bytes.
-
numberOfLeadingZeros
public int numberOfLeadingZeros()- Specified by:
numberOfLeadingZerosin interfaceorg.apache.tuweni.bytes.Bytes
-
bitLength
public int bitLength()- Specified by:
bitLengthin interfaceorg.apache.tuweni.bytes.Bytes
-
max
Description copied from interface:UInt256ValueReturn 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:
maxin interfaceUInt256Value<UInt256>- Returns:
- The max value for this type.
-