pg-libphonenumber/mask.h

15 lines
490 B
C
Raw Normal View History

2015-07-27 20:52:38 +00:00
#include <algorithm>
template<typename T> constexpr T mask(size_t bits, size_t offset = 0) {
2017-02-18 17:11:41 +00:00
return (((T)1 << bits) - 1) << offset;
}
template<typename T> constexpr T getMasked(T data, size_t bits, size_t offset) {
2017-02-18 17:11:41 +00:00
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) {
2017-02-18 17:11:41 +00:00
return (data & ~mask<T>(bits, offset)) | ((value & mask<T>(bits)) << offset);
}