Mixed Type Operations
Conversions
The ability to convert between the two types via static_cast is available as documented in the above class descriptions.
Comparisons and Arithmetics
The following operations are disabled by default.
Since we cannot enforce -Wsign-conversion and -Wsign-compare through the compiler, we instead static_assert that the operation is unavailable.
This removes a common source of error (search "Sign Conversion" on Stack Overflow).
To enable these operations, define the appropriate configuration macros before including any library headers:
-
BOOST_INT128_ALLOW_SIGN_COMPARE- enables comparison operators between signed and unsigned types -
BOOST_INT128_ALLOW_SIGN_CONVERSION- enables arithmetic operators between signed and unsigned types (impliesBOOST_INT128_ALLOW_SIGN_COMPARE)
namespace boost {
namespace int128 {
//=====================================
// Comparison Operators
//=====================================
constexpr bool operator==(uint128_t lhs, int128_t rhs);
constexpr bool operator==(int128_t lhs, uint128_t rhs);
constexpr bool operator!=(uint128_t lhs, int128_t rhs);
constexpr bool operator!=(int128_t lhs, uint128_t rhs);
constexpr bool operator<(uint128_t lhs, int128_t rhs);
constexpr bool operator<(int128_t lhs, uint128_t rhs);
constexpr bool operator<=(uint128_t lhs, int128_t rhs);
constexpr bool operator<=(int128_t lhs, uint128_t rhs);
constexpr bool operator>(uint128_t lhs, int128_t rhs);
constexpr bool operator>(int128_t lhs, uint128_t rhs);
constexpr bool operator>=(uint128_t lhs, int128_t rhs);
constexpr bool operator>=(int128_t lhs, uint128_t rhs);
//=====================================
// Arithmetic Operators
//=====================================
constexpr uint128_t operator+(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator+(int128_t lhs, uint128_t rhs);
constexpr uint128_t operator-(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator-(int128_t lhs, uint128_t rhs);
constexpr uint128_t operator*(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator*(int128_t lhs, uint128_t rhs);
constexpr uint128_t operator/(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator/(int128_t lhs, uint128_t rhs);
constexpr uint128_t operator%(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator%(int128_t lhs, uint128_t rhs);
} // namespace int128
} // namespace boost
Comparisons
If you define BOOST_INT128_ALLOW_SIGN_COMPARE, the operators have the following behavior.
Equality
constexpr bool operator==(uint128_t lhs, int128_t rhs);
constexpr bool operator==(int128_t lhs, uint128_t rhs);
If the int128_t argument is less than 0 returns false.
Otherwise, returns the same as static_cast<uint128_t>(lhs) == static_cast<uint128_t>(rhs).
Inequality
constexpr bool operator!=(uint128_t lhs, int128_t rhs);
constexpr bool operator!=(int128_t lhs, uint128_t rhs);
If the int128_t argument is less than 0 returns false.
Otherwise, returns the same as static_cast<uint128_t>(lhs) != static_cast<uint128_t>(rhs).
Less Than
constexpr bool operator<(uint128_t lhs, int128_t rhs);
constexpr bool operator<(int128_t lhs, uint128_t rhs);
If lhs is type int128_t returns true if lhs < 0
If rhs is type int128_t returns false if rhs < 0
Otherwise, returns the same as static_cast<uint128_t>(lhs) < static_cast<uint128_t>(rhs).
Less Than or Equal To
constexpr bool operator<=(uint128_t lhs, int128_t rhs);
constexpr bool operator<=(int128_t lhs, uint128_t rhs);
If lhs is type int128_t returns true if lhs < 0
If rhs is type int128_t returns false if rhs < 0
Otherwise, returns the same as static_cast<uint128_t>(lhs) <= static_cast<uint128_t>(rhs).
Greater Than
constexpr bool operator>(uint128_t lhs, int128_t rhs);
constexpr bool operator>(int128_t lhs, uint128_t rhs);
If lhs is type int128_t returns false if lhs < 0
If rhs is type int128_t returns true if rhs < 0
Otherwise, returns the same as static_cast<uint128_t>(lhs) > static_cast<uint128_t>(rhs).
Less Than or Equal To
constexpr bool operator>=(uint128_t lhs, int128_t rhs);
constexpr bool operator>=(int128_t lhs, uint128_t rhs);
If lhs is type int128_t returns false if lhs < 0
If rhs is type int128_t returns true if rhs < 0
Otherwise, returns the same as static_cast<uint128_t>(lhs) >= static_cast<uint128_t>(rhs).
Arithmetic
If you define BOOST_INT128_ALLOW_SIGN_CONVERSION, the operators have the following behavior.
Addition
constexpr uint128_t operator+(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator+(int128_t lhs, uint128_t rhs);
Returns the same as static_cast<uint128_t>(lhs) + static_cast<uint128_t>(rhs)
Subtraction
constexpr uint128_t operator-(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator-(int128_t lhs, uint128_t rhs);
Returns the same as static_cast<uint128_t>(lhs) - static_cast<uint128_t>(rhs)
Multiplication
constexpr uint128_t operator*(uint128_t lhs, int128_t rhs);
constexpr uint128_t operator*(int128_t lhs, uint128_t rhs);
Returns the same as static_cast<uint128_t>(lhs) * static_cast<uint128_t>(rhs)