DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Static Public Member Functions | Static Public Attributes
DGtal::Bits Struct Reference

#include <Bits.h>

Static Public Member Functions

template<typename T >
static std::string bitString (T value, unsigned nbBits=0)
template<typename T >
static T mask (unsigned nthBit)
template<typename T >
static bool getBit (T key, unsigned nthBit)
template<typename T >
static T firstSetBit (T val)
template<typename T >
static T firstUnsetBit (T val)
template<typename T >
static unsigned int nbSetBits (T val)
static unsigned int nbSetBits (DGtal::uint8_t val)
static unsigned int nbSetBits (DGtal::uint16_t val)
static unsigned int nbSetBits (DGtal::uint32_t val)
static unsigned int nbSetBits (DGtal::uint64_t val)
static unsigned int indexInSetBits (DGtal::uint8_t n, unsigned int b)
static unsigned int indexInSetBits (DGtal::uint16_t n, unsigned int b)
static unsigned int indexInSetBits (DGtal::uint32_t n, unsigned int b)
static unsigned int indexInSetBits (DGtal::uint64_t n, unsigned int b)
static unsigned int leastSignificantBit (DGtal::uint8_t n)
static unsigned int leastSignificantBit (DGtal::uint16_t n)
static unsigned int leastSignificantBit (DGtal::uint32_t n)
static unsigned int leastSignificantBit (DGtal::uint64_t n)

Static Public Attributes

static const DGtal::uint8_t myBitCount [256]
static const DGtal::uint8_t myLSB [256]
static const DGtal::uint8_t myIndexInSetBits [8][256]

Detailed Description

Definition at line 41 of file Bits.h.


Member Function Documentation

template<typename T >
static std::string DGtal::Bits::bitString ( value,
unsigned  nbBits = 0 
)
inlinestatic

Bits Structs grouping all the functions of this tiny library for bitwise calculation.

Todo:
Check that T is CInteger.

Returns a string containing value's bits. Mainly designed for debugging purposes.

Parameters:
valueThe value that you need to dipslay as a bit string.
nbBitsnumber of bits to be displayed. If equal to 0, the number of bits will correspond to the size of the type T.

Definition at line 59 of file Bits.h.

Referenced by DGtal::ImageContainerByHashTree< TDomain, TValue, THashKey >::checkIntegrity(), DGtal::ImageContainerByHashTree< TDomain, TValue, THashKey >::printInternalState(), DGtal::ImageContainerByHashTree< TDomain, TValue, THashKey >::printState(), and DGtal::ImageContainerByHashTree< TDomain, TValue, THashKey >::printTree().

{
std::string bitStr;
/*MinFunctor<unsigned int> min;*/
// if the requested number of bit is 0, use the size of the data type instead
if(nbBits == 0) nbBits = sizeof(T)*8;
int i = (int)(std::min((DGtal::int64_t)sizeof(T)*8-1, (DGtal::int64_t)nbBits-1));
for(; i>=0; i--)
{
T mask = ((T)1) << i; // if you take these parenthesis out,
// a mountain of incredible runtime
// errors will jump on you.(I warned
// ya !)
if(value & mask)
bitStr += "1" ;
else
bitStr += "0" ;
}
return bitStr;
}
template<typename T >
static T DGtal::Bits::firstSetBit ( val)
inlinestatic

Returns a value such that only its bit corresponding to the first (least important) set bit of val, is set.

Definition at line 111 of file Bits.h.

Referenced by DGtal::Labels< L, TWord >::getLabels(), and DGtal::Labels< L, TWord >::ConstEnumerator::operator++().

{
return ( (val & -val) | (val & (~val + 1)) );
}
template<typename T >
static T DGtal::Bits::firstUnsetBit ( val)
inlinestatic

Returns a value such that only its bit corresponding to the first (least important) unset bit of val, is set.

Definition at line 122 of file Bits.h.

{
return ~val & (val + 1);
}
template<typename T >
static bool DGtal::Bits::getBit ( key,
unsigned  nthBit 
)
inlinestatic

Returns the state of key's nthBit bit.

Definition at line 100 of file Bits.h.

{
return ( key & mask<T>(nthBit) );
}
static unsigned int DGtal::Bits::indexInSetBits ( DGtal::uint8_t  n,
unsigned int  b 
)
inlinestatic

Specialization for uint8_t.

Set bits are numbered from 1 to x when reading the word from the least significant to the most significant bit. This number is the index of bit b in the number n.

Parameters:
ba bit index in 0..7
na number in 0..255
Returns:
this index or 0 if the bit is not set.

Definition at line 209 of file Bits.h.

References myIndexInSetBits.

Referenced by DGtal::Labels< L, TWord >::index(), and indexInSetBits().

{
ASSERT( b < 8 );
return myIndexInSetBits[ b ][ n ];
}
static unsigned int DGtal::Bits::indexInSetBits ( DGtal::uint16_t  n,
unsigned int  b 
)
inlinestatic

Specialization for uint16_t.

Set bits are numbered from 1 to x when reading the word from the least significant to the most significant bit. This number is the index of bit b in the number n.

Parameters:
ba bit index in 0..15
na number in 0..65535
Returns:
this index or 0 if the bit is not set.

Definition at line 227 of file Bits.h.

References indexInSetBits(), and nbSetBits().

{
ASSERT( b < 16 );
if ( b < 8 )
return indexInSetBits( static_cast<DGtal::uint8_t>( n & 0xff ), b );
else
{
unsigned int idx = indexInSetBits( static_cast<DGtal::uint8_t>( n >> 8 ), b - 8 );
return ( idx == 0 )
? 0 // bit b is not set
: idx + nbSetBits( static_cast<DGtal::uint8_t>( n & 0xff ) );
}
}
static unsigned int DGtal::Bits::indexInSetBits ( DGtal::uint32_t  n,
unsigned int  b 
)
inlinestatic

Specialization for uint32_t.

Set bits are numbered from 1 to x when reading the word from the least significant to the most significant bit. This number is the index of bit b in the number n.

Parameters:
ba bit index in 0..31
na number in 0..2^32-1
Returns:
this index or 0 if the bit is not set.

Definition at line 253 of file Bits.h.

References indexInSetBits(), and nbSetBits().

{
ASSERT( b < 32 );
if ( b < 16 )
return indexInSetBits( static_cast<DGtal::uint16_t>( n & 0xffff ), b );
else
{
unsigned int idx = indexInSetBits( static_cast<DGtal::uint16_t>( n >> 16 ), b - 16 );
return ( idx == 0 )
? 0 // bit b is not set
: idx + nbSetBits( static_cast<DGtal::uint16_t>( n & 0xffff ) );
}
}
static unsigned int DGtal::Bits::indexInSetBits ( DGtal::uint64_t  n,
unsigned int  b 
)
inlinestatic

Specialization for uint64_t.

Set bits are numbered from 1 to x when reading the word from the least significant to the most significant bit. This number is the index of bit b in the number n.

Parameters:
ba bit index in 0..63
na number in 0..2^64-1
Returns:
this index or 0 if the bit is not set.

Definition at line 279 of file Bits.h.

References indexInSetBits(), and nbSetBits().

{
ASSERT( b < 64 );
if ( b < 32 )
return indexInSetBits( static_cast<DGtal::uint32_t>( n & 0xffffffffLL ), b );
else
{
unsigned int idx = indexInSetBits( static_cast<DGtal::uint32_t>( n >> 32 ), b - 32 );
return ( idx == 0 )
? 0 // bit b is not set
: idx + nbSetBits( static_cast<DGtal::uint32_t>( n & 0xffffffffLL ) );
}
}
static unsigned int DGtal::Bits::leastSignificantBit ( DGtal::uint8_t  n)
inlinestatic
Parameters:
nany number
Returns:
the index (0..) of the least significant bit.

Definition at line 298 of file Bits.h.

References myLSB.

Referenced by DGtal::Labels< L, TWord >::getLabels(), leastSignificantBit(), and DGtal::Labels< L, TWord >::ConstEnumerator::operator++().

{
return myLSB[ n ];
}
static unsigned int DGtal::Bits::leastSignificantBit ( DGtal::uint16_t  n)
inlinestatic
Parameters:
nany number
Returns:
the index (0..) of the least significant bit.

Definition at line 308 of file Bits.h.

References leastSignificantBit().

{
return ( n & 0xff )
: 8 + leastSignificantBit( (DGtal::uint8_t) (n>>8) );
}
static unsigned int DGtal::Bits::leastSignificantBit ( DGtal::uint32_t  n)
inlinestatic
Parameters:
nany number
Returns:
the index (0..) of the least significant bit.

Definition at line 320 of file Bits.h.

References leastSignificantBit().

{
return ( n & 0xffff )
: 16 + leastSignificantBit( (DGtal::uint16_t) (n>>16) );
}
static unsigned int DGtal::Bits::leastSignificantBit ( DGtal::uint64_t  n)
inlinestatic
Parameters:
nany number
Returns:
the index (0..) of the least significant bit.

Definition at line 332 of file Bits.h.

References leastSignificantBit().

{
return ( n & 0xffffffffLL )
: 32 + leastSignificantBit( (DGtal::uint32_t) (n>>32) );
}
template<typename T >
static T DGtal::Bits::mask ( unsigned  nthBit)
inlinestatic

Returns an value which bits are of the form 0..010..0 with the nthBit equal to 1.

Definition at line 91 of file Bits.h.

Referenced by DGtal::ImageContainerByHashTree< TDomain, TValue, THashKey >::getCoordinatesFromKey().

{
return static_cast<T>(static_cast<T>(1) << nthBit);
}
template<typename T >
static unsigned int DGtal::Bits::nbSetBits ( val)
inlinestatic

Returns the amount of set bits in val.

Definition at line 132 of file Bits.h.

Referenced by DGtal::Labels< L, TWord >::count(), DGtal::Labels< L, TWord >::index(), indexInSetBits(), and nbSetBits().

{
#ifdef TRACE_BITS
std::cerr << "unsigned int nbSetBits(T val)" << std::endl;
#endif
unsigned int i = 0;
for ( ; val; ++i) {val ^= val & -val; }
return i;
}
static unsigned int DGtal::Bits::nbSetBits ( DGtal::uint8_t  val)
inlinestatic

Overloading for type uint8_t Returns the amount of set bits in val.

Definition at line 147 of file Bits.h.

References myBitCount.

{
#ifdef TRACE_BITS
std::cerr << "unsigned int nbSetBits( DGtal::uint8_t val )" << std::endl;
#endif
return myBitCount[ val ];
}
static unsigned int DGtal::Bits::nbSetBits ( DGtal::uint16_t  val)
inlinestatic

Overloading for type uint16_t Returns the amount of set bits in val.

Definition at line 160 of file Bits.h.

References nbSetBits().

{
#ifdef TRACE_BITS
std::cerr << "unsigned int nbSetBits( DGtal::uint16_t val )" << std::endl;
#endif
return nbSetBits( static_cast<DGtal::uint8_t>( val & 0xff ) )
+ nbSetBits( static_cast<DGtal::uint8_t>( val >> 8 ) );
}
static unsigned int DGtal::Bits::nbSetBits ( DGtal::uint32_t  val)
inlinestatic

Overloading for type uint32_t Returns the amount of set bits in val.

Definition at line 174 of file Bits.h.

References nbSetBits().

{
#ifdef TRACE_BITS
std::cerr << "unsigned int nbSetBits( DGtal::uint32_t val )" << std::endl;
#endif
return nbSetBits( static_cast<DGtal::uint16_t>( val & 0xffff ) )
+ nbSetBits( static_cast<DGtal::uint16_t>( val >> 16 ) );
}
static unsigned int DGtal::Bits::nbSetBits ( DGtal::uint64_t  val)
inlinestatic

Overloading for type uint64_t Returns the amount of set bits in val.

Definition at line 188 of file Bits.h.

References nbSetBits().

{
#ifdef TRACE_BITS
std::cerr << "unsigned int nbSetBits( DGtal::uint64_t val )" << std::endl;
#endif
return nbSetBits( static_cast<DGtal::uint32_t>( val & 0xffffffffLL ) )
+ nbSetBits( static_cast<DGtal::uint32_t>( val >> 32 ) );
}

Field Documentation

const DGtal::uint8_t DGtal::Bits::myBitCount
static

Lookup table for counting the number of bits set to 1 in a byte. ( Taken from STL <bitset> )

Definition at line 344 of file Bits.h.

Referenced by nbSetBits().

const DGtal::uint8_t DGtal::Bits::myIndexInSetBits
static

Usage: myIndexInSetBits[ b ][ n ]

  • b in 0..7
  • n in 0..255 Set bits are numbered from 1 to x when reading the word from the least significant to the most significant bit. This number is the index of bit b in the number n. return this index or 0 if the bit is not set.

Definition at line 360 of file Bits.h.

Referenced by indexInSetBits().

const DGtal::uint8_t DGtal::Bits::myLSB
static

Lookup table for finding the least significant bit.

Lookup table for finding the least significant bit.

NB: Can also be obtained with:

     

Definition at line 349 of file Bits.h.

Referenced by leastSignificantBit().


The documentation for this struct was generated from the following files: