This library contains some bitwise template functions for competitive programming. You can research and modify it as you wish.
Swap two arithmetic variables without temporary buffer:
cppbwh::xorswap( x, y );Reverse all bits in arithmetic variable:
cppbwh::reverse( x ); // 00101000 -> 00010100Getting lowest set bit of a number:
cppbwh::lsb( x ); // 01010010 -> 00000010Getting highest set bit of a number:
cppbwh::lsb( x ); // 01010010 -> 01000000Get binary code of an arithmetic variable:
cppbwh::tobin( x, out, false ); // 5 -> "00000101"
cppbwh::tobin( x, out, true ); // 5 -> "101"Swap odd and even bits:
cppbwh::swapn( x ); // 10100010 -> 01010001Log2 for unsigned integer variables:
cppbwh::floor_log2( x ); // 8 -> 3, 9 -> 3Example of multiplication integer by 3.5:
cppbwh::mul_3_5( x ); // 4 -> 14Next power of 2:
cppbwh::nlpo2( x ); // 5 -> 8, 14 -> 16, 27 -> 32Set, clear and check bit at n’th position:
cppbwh::setbit( x, 5 ); // Set bit at 5
cppbwh::clearbit( x, 5 ); // Clear bit at 5
cppbwh::checkbit( x, 5 ); // Check bit at 5 and return true or falseRotate bits in a number:
cppbwh::rotate_left( x, 3 ); // 00101000 -> 01000001
cppbwh::rotate_right( x, 3 ); // 00101000 -> 00000101Convert data to hex format:
cppbwh::hex( const char* in, unsigned int length, char* out );
cppbwh::hex( const char* in, unsigned int length, std::string& out );
cppbwh::hex( const T x, char* out, bool skip_zero = false );
cppbwh::hex( const T x, std::string& out, bool skip_zero = false );Convert data from hex to bin format:
cppbwh::unhex( const char* in, unsigned int length, char* out );
cppbwh::unhex( const char* in, unsigned int length, std::string& out );
cppbwh::unhex( const char* in, T& x );See source code for more details.
g++ -std=c++14 -m64 -O2 -mavx cppbwh_test.cpp -o cppbwh_test