32 #include "DGtal/base/Common.h"
33 #include "DGtal/base/BasicFunctors.h"
34 #include "DGtal/base/ExpressionTemplates.h"
59 static std::string
bitString(T value,
unsigned nbBits = 0)
65 if(nbBits == 0) nbBits =
sizeof(T)*8;
91 static inline T
mask(
unsigned nthBit)
93 return static_cast<T
>(
static_cast<T
>(1) << nthBit);
100 static inline bool getBit(T key,
unsigned nthBit)
102 return ( key & mask<T>(nthBit) );
110 template <
typename T>
113 return ( (val & -val) | (val & (~val + 1)) );
121 template <
typename T>
124 return ~val & (val + 1);
131 template <
typename T>
135 std::cerr <<
"unsigned int nbSetBits(T val)" << std::endl;
138 for ( ; val; ++i) {val ^= val & -val; }
150 std::cerr <<
"unsigned int nbSetBits( DGtal::uint8_t val )" << std::endl;
163 std::cerr <<
"unsigned int nbSetBits( DGtal::uint16_t val )" << std::endl;
165 return nbSetBits( static_cast<DGtal::uint8_t>( val & 0xff ) )
166 +
nbSetBits( static_cast<DGtal::uint8_t>( val >> 8 ) );
177 std::cerr <<
"unsigned int nbSetBits( DGtal::uint32_t val )" << std::endl;
179 return nbSetBits( static_cast<DGtal::uint16_t>( val & 0xffff ) )
180 +
nbSetBits( static_cast<DGtal::uint16_t>( val >> 16 ) );
191 std::cerr <<
"unsigned int nbSetBits( DGtal::uint64_t val )" << std::endl;
193 return nbSetBits( static_cast<DGtal::uint32_t>( val & 0xffffffffLL ) )
194 +
nbSetBits( static_cast<DGtal::uint32_t>( val >> 32 ) );
231 return indexInSetBits( static_cast<DGtal::uint8_t>( n & 0xff ), b );
234 unsigned int idx =
indexInSetBits( static_cast<DGtal::uint8_t>( n >> 8 ), b - 8 );
237 : idx +
nbSetBits( static_cast<DGtal::uint8_t>( n & 0xff ) );
257 return indexInSetBits( static_cast<DGtal::uint16_t>( n & 0xffff ), b );
260 unsigned int idx =
indexInSetBits( static_cast<DGtal::uint16_t>( n >> 16 ), b - 16 );
263 : idx +
nbSetBits( static_cast<DGtal::uint16_t>( n & 0xffff ) );
283 return indexInSetBits( static_cast<DGtal::uint32_t>( n & 0xffffffffLL ), b );
286 unsigned int idx =
indexInSetBits( static_cast<DGtal::uint32_t>( n >> 32 ), b - 32 );
289 : idx +
nbSetBits( static_cast<DGtal::uint32_t>( n & 0xffffffffLL ) );
322 return ( n & 0xffff )
334 return ( n & 0xffffffffLL )