Geting/Setting individual Bits

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

See Also: unsigned_int<> ...

Tests

get_bit()

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

imp::signed_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

set_bit()

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

imp::signed_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