DGtal 0.3.0

Bits.h

Go to the documentation of this file.
00001 
00017 #pragma once
00018 
00026 #ifndef BITS_HPP
00027 #define BITS_HPP
00028 
00029 #include <string>
00030 #include "DGtal/base/Common.h"
00031 #include "DGtal/base/BasicFunctors.h"
00032 
00033 
00034 namespace DGtal
00035 {
00036  
00040   template <int X, unsigned exponent> class POW
00041   {
00042   public:       enum{ VALUE = X* POW<X, exponent-1>::VALUE};
00043   };
00044   template <int X > class POW<X, 1>
00045   {
00046   public:       enum{ VALUE = X };
00047   };
00048 
00052   template <int X>
00053   class LOG2
00054   {
00055   public:       enum{ VALUE = 1 +  LOG2<X / 2>::VALUE};
00056   };
00057   template <> class LOG2<2>
00058   {
00059   public:       enum{ VALUE = 1 };
00060   };
00061   template <> class LOG2<1>
00062   {
00063   public:       enum{ VALUE = 0 };
00064   };
00065 
00066   struct Bits
00067   {
00083     template <typename T>
00084     static std::string bitString(T value, unsigned nbBits = 0)
00085     {
00086       std::string bitStr;
00087       MinFunctor<unsigned int> min;
00088 
00089       // if the requested number of bit is 0, use the size of the data type instead
00090       if(nbBits == 0) nbBits = sizeof(T)*8;
00091       int i = min(sizeof(T)*8-1, nbBits-1);
00092 
00093       for(; i>=0; i--)
00094         {
00095           T mask = ((T)1) << i; // if you take these parenthesis out,
00096                                 // a mountain of incredible runtime
00097                                 // errors will jump on you.(I warned
00098                                 // ya !)
00099           if(value & mask)
00100             bitStr += "1" ;
00101           else
00102             bitStr += "0" ;
00103         }
00104       return bitStr;
00105     }
00106 
00107 
00108     // ---------------------------------------------------------------------
00109     //  Other functions
00110     // ---------------------------------------------------------------------
00111 
00115     template<typename T> 
00116     static inline T mask(unsigned nthBit)
00117     {
00118       return static_cast<T>(static_cast<T>(1) << nthBit); 
00119     }
00120 
00124     template <typename T>
00125     static inline bool getBit(T key, unsigned nthBit)
00126     {
00127       return ( key & mask<T>(nthBit) );
00128     }
00129 
00130   
00135     template <typename T>
00136     static  T firstSetBit(T val)
00137     {
00138       return (val & -val | val & (~val + 1));
00139     }
00140 
00141 
00146     template <typename T>
00147     static T firstUnsetBit(T val)
00148     {
00149       return ~val & (val + 1);
00150     }
00151 
00152 
00156     template <typename T>
00157     static unsigned nbSetBits(T val)
00158     {
00159       unsigned i = 0;
00160       for ( ; val; ++i) {val ^= val & -val; }
00161       return i;
00162     }
00163 
00164 
00165   };//struct
00166 }
00167 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines