Uints are immutable, so all operations create a new object as the result of the operation.

Arithmetic operations

Adding and subtracting

Since these are bound integers, they will overflow and underflow if the operation returns a value outside the boundaries - for a Uint256, 0 to 2^256.

For this reason, the API also contains exact methods that throw exceptions if the operations overflows or underflows.

Multiplying and dividing

Additionally, the method divideCeil(other) divides integers but returns the ceiling of the rounding.

UInt256 result = UInt256.valueOf(12L).divide(UInt256.valueOf(5L)); // returns 2
UInt256 resultCeiling = UInt256.valueOf(12L).divideCeil(UInt256.valueOf(5L)); // returns 3

Modulus

You can use the method mod(divisor) to get the modulus of the value by the divisor.

The method mod0 is more forgiving - if you divide by zero, it will return zero instead of throwing an exception.

Power

The method pow(exponent) returns a value that is `value^exponent mod 2^256.

Boolean operations

You can use the following methods to perform boolean operations:

Shifting bytes

You can shift right and left the bits of the underlying bytes object by a given distance.

This is equivalent to the <<< or >>> operators in Java.