2015-07-27 20:52:38 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-07-22 19:23:40 +00:00
|
|
|
template<typename T> constexpr T mask(size_t bits, size_t offset = 0) {
|
|
|
|
return (((T)1 << bits) - 1) << offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> constexpr T getMasked(T data, size_t bits, size_t offset) {
|
|
|
|
return (data >> offset) & mask<T>(bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: support typeof(data) != typeof(value)?
|
|
|
|
template<typename T> constexpr T setMasked(T data, T value, size_t bits, size_t offset) {
|
|
|
|
return (data & ~mask<T>(bits, offset)) | ((value & mask<T>(bits)) << offset);
|
|
|
|
}
|