Math: Add SetBit and TestBit

This commit is contained in:
Jérôme Leclercq 2020-12-30 18:14:41 +01:00
parent 386350b96c
commit a1c15a8c16
2 changed files with 16 additions and 0 deletions

View File

@ -61,7 +61,9 @@ namespace Nz
template<typename T> constexpr bool NumberEquals(T a, T b, T maxDifference);
inline std::string NumberToString(long long number, UInt8 radix = 10);
template<typename T> constexpr T RadianToDegree(T radians);
template<typename T> T SetBit(T number, T bit);
inline long long StringToNumber(const std::string_view& str, UInt8 radix = 10, bool* ok = nullptr);
template<typename T> bool TestBit(T number, T bit);
template<typename T> constexpr T ToDegrees(T angle);
template<typename T> constexpr T ToRadians(T angle);
}

View File

@ -624,6 +624,13 @@ namespace Nz
return radians * T(180.0/M_PI);
}
template<typename T>
T SetBit(T number, T bit)
{
NazaraAssert(bit < sizeof(number)* CHAR_BIT, "bit index out of range");
return number |= (T(1) << bit);
}
/*!
* \ingroup math
* \brief Converts the string to number
@ -689,6 +696,13 @@ namespace Nz
return (negative) ? -static_cast<long long>(total) : total;
}
template<typename T>
bool TestBit(T number, T bit)
{
NazaraAssert(bit < sizeof(number) * CHAR_BIT, "bit index out of range");
return number & (T(1) << bit);
}
/*!
* \ingroup math
* \brief Gets the degree from unit and convert it according to NAZARA_MATH_ANGLE_RADIAN