Geting/Setting individual Bits

The library provides two (x2) functions for getting and setting individual bits in an unsigned_int<>, also (in namespace imp::adl) are two generic function's for getting.setting bits from other (inbuilt) unsigned types.

Tests

get_bit()

bool get_bit( unsigned_int< ... > const &, unsigned offset );

unsigned_int< 16 * 8 > a( "0xFFEEDDCCBBAA99887766554433221100" );

for ( unsigned i = 0; i < 16; ++i )
{
  unsigned char b = i | (i << 4 );
  for ( unsigned j = 0; j < 8; ++j )
  {
    bool r = ( b & (1U << j) ) ? true : false;
    CHECK( r == get_bit( a, (i * 8) + j ) );
  }
}

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

Ok

get_bit() - inbuilt

template < Int > bool get_bit( Int const &, unsigned offset );

unsigned long a( 0x33221100L );

for ( unsigned i = 0; i < (UINT_BITS / 8); ++i )
{
  unsigned r = (i << 4) | i;
  for ( unsigned j = 0; j < 8; ++j )
  {
    /** ADL cant find *inbuilts*
    */
    using namespace imp::adl;
    
    bool b = get_bit( a, (i * 8) + j );
    bool bit = ( r & ( 1U << j ) ) ? true : false;
    CHECK( b == bit );
  }
}

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

Ok

set_bit()

unsigned_int< ... > &set_bit( unsigned_int< ... > &, usigned offset, bool value );

unsigned_int< 16 * 8 > a( "0xFFEEDDCCBBAA99887766554433221100" ), b;

for ( unsigned i = 0; i < 16; ++i )
{
  unsigned v = (i << 4) | i;
  for ( unsigned j = 0; j < 8; ++j )
  {
    set_bit( b, (i * 8) + j, v & ( 1U << j ) );
  }
}
CHECK( a == b );
std::cout << std::hex << b << std::endl;

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

ffeeddccbbaa99887766554433221100
Ok

set_bit() - inbuilt

template < Int > Int &set_bit( Int &, unsigned offset, bool value );

unsigned long a( 0x33221100L ), b = 0;

for ( unsigned i = 0; i < (UINT_BITS / 8); ++i )
{
  unsigned v = (i << 4) | i;
  for ( unsigned j = 0; j < 8; ++j )
  {
    /** ADL cant find *inbuilts*
    */
    using namespace imp::adl;
    
    set_bit( b, (i * 8) + j, v & ( 1U << j ) );
  }
}
CHECK( a == b );
std::cout << std::hex << b << std::endl;

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

33221100
Ok