OSDN Git Service

math/bit_util.hpp: create_bitmask 実装してみたけどちゃんとならねぇw
authorMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 12:36:00 +0000 (21:36 +0900)
committerMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 12:36:00 +0000 (21:36 +0900)
roast/include/roast/tp/math/bit_util.hpp

index a5283e7..d38829a 100644 (file)
@@ -8,16 +8,30 @@
 
 namespace roast
 {
-       template <typename T>
-       T bit_extract(const T& target, unsigned int start_bit, unsigned int length)
+       namespace tp
        {
-               ::std::bitset<sizeof(T) * 8> bs(target);
-               T ret;
-               for(unsigned int i=start_bit; i < start_bit+length; i++)
+               ////////////////
+               
+               template <unsigned int START_BIT, unsigned int LENGTH>
+               struct create_bitmask
                {
-                       ret |= (bs[i] << (i-start_bit));
-               }
-               return ret;
+               private:
+                       template <unsigned int START_BIT_I, unsigned int LENGTH_I>
+                       struct _impl
+                       {
+                               static const int value = (1 << START_BIT_I) +
+                                                                                _impl<START_BIT_I+1, LENGTH_I-1>::value;
+                       };
+                       template <unsigned int START_BIT_I>
+                       struct _impl<START_BIT_I, 0>
+                       {
+                               static const int value = 1 << START_BIT_I;
+                       };
+               public:
+                       static const int value = _impl<START_BIT, LENGTH>::value;
+               };
+               
+               ////////////////
        }
 }