#include <iostream>
#include <stdint.h>
#include <array>
namespace details
{
constexpr uint8_t bit( unsigned byte, unsigned n ) {
return (byte>>n)&1;
}
constexpr uint8_t even_bits(uint8_t byte) {
return bit(byte, 0) | (bit(byte, 2)<<1) | (bit(byte, 4)<<2) | (bit(byte, 6)<<3);
}
constexpr uint8_t odd_bits(uint8_t byte) {
return even_bits(byte/2);
}
template<unsigned...>struct indexes{using type=indexes;};
template<unsigned Max,unsigned...Is>struct make_indexes:make_indexes<Max-1,Max-1,Is...>{};
template<unsigned...Is>struct make_indexes<0,Is...>:indexes<Is...>{};
template<unsigned Max>using make_indexes_t=typename make_indexes<Max>::type;
template<unsigned...Is>
constexpr std::array< uint8_t, 256 > even_bit_table( indexes<Is...> ) {
return {
even_bits(Is)...
};
}
template<unsigned...Is>
constexpr std::array< uint8_t, 256 > odd_bit_table( indexes<Is...> ) {
return {
odd_bits(Is)...
};
}
constexpr std::array< uint8_t, 256 > even_bit_table() {
return even_bit_table( make_indexes_t<256>{} );
}
constexpr std::array< uint8_t, 256 > odd_bit_table() {
return odd_bit_table( make_indexes_t<256>{} );
}
static constexpr auto etable = even_bit_table();
static constexpr auto otable = odd_bit_table();
}
uint8_t constexpr even_bits( uint16_t in ) {
return details::etable[(uint8_t)in] | ((details::etable[(uint8_t)(in>>8)])<<4);
}
uint8_t constexpr odd_bits( uint16_t in ) {
return details::otable[(uint8_t)in] | ((details::otable[(uint8_t)(in>>8)])<<4);
}
template<class A>
void print_array( A&& a ) {
std::size_t cnt = 0;
std::cout << "[ ";
for (auto&& e:a) {
if (cnt!=0)
std::cout << ", ";
std::cout << (int)e;
++cnt;
if (!(cnt%8))
std::cout << "\n ";
}
std::cout << "]\n";
}
void dump( uint16_t x )
{
std::cout << (int)odd_bits(x) << "\n";
std::cout << (int)odd_bits(x<<1) << "\n";
std::cout << (int)odd_bits(x<<2) << "\n";
std::cout << (int)odd_bits(x<<3) << "\n";
std::cout << (int)odd_bits(x<<4) << "\n";
std::cout << (int)odd_bits(x<<5) << "\n";
std::cout << (int)odd_bits(x<<6) << "\n";
std::cout << (int)odd_bits(x<<7) << "\n";
}
int main() {
// print_array(otable);
// print_array(etable);
dump(1);
dump(1<<8);
// your code goes here
return 0;
}