What is the most efficient algorithm to achieve the following:
0010 0000 => 0000 0100
The conversion is from MSB->LSB to LSB->MSB. All bits
You might want to use the standard template library. It might be slower than the above mentioned code. However, it seems to me clearer and easier to understand.
#include
#include
template
const std::bitset reverse(const std::bitset& ordered)
{
std::bitset reversed;
for(size_t i = 0, j = N - 1; i < N; ++i, --j)
reversed[j] = ordered[i];
return reversed;
};
// test the function
int main()
{
unsigned long num;
const size_t N = sizeof(num)*8;
std::cin >> num;
std::cout << std::showbase << std::hex;
std::cout << "ordered = " << num << std::endl;
std::cout << "reversed = " << reverse(num).to_ulong() << std::endl;
std::cout << "double_reversed = " << reverse(reverse(num)).to_ulong() << std::endl;
}