bitwise: add bit reverse operation
This commit is contained in:
parent
1a461eddf0
commit
d78df821bf
17
bitwise.hpp
17
bitwise.hpp
@ -52,5 +52,22 @@ rotater (const T &value, size_t magnitude) {
|
||||
|
||||
#undef MODT
|
||||
|
||||
// TODO: make constexpr for C++14
|
||||
template <typename T>
|
||||
T
|
||||
reverse (T value) {
|
||||
T out = value;
|
||||
|
||||
size_t bits = sizeof (value) * 8 - 1;
|
||||
for (value >>= 1; value; value >>= 1) {
|
||||
out <<= 1;
|
||||
out |= value & 0x01;
|
||||
--bits;
|
||||
}
|
||||
|
||||
out <<= bits;
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user