Advertisement
rizky_herucakra

multiply with power of 2

Jul 1st, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  TestMulti
  4. //
  5. //  Created by Rizky Herucakra on 7/1/13.
  6. //  Copyright (c) 2013 Rizky Herucakra. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10.  
  11. template<size_t v> struct antilog2;
  12.  
  13. template<> struct antilog2<1>{
  14.     enum { result = 0};
  15. };
  16.  
  17. template<> struct antilog2<2>{
  18.     enum { result = 1};
  19. };
  20.  
  21. template<> struct antilog2<4>{
  22.     enum { result = 2};
  23. };
  24. template<> struct antilog2<8>{
  25.     enum { result = 3};
  26. };
  27. template<> struct antilog2<16>{
  28.     enum { result = 4};
  29. };
  30.  
  31.  
  32.  
  33. template <size_t m,typename T>
  34. T mul_sh(T val){
  35.     return val << antilog2<m>::result;
  36. }
  37.  
  38.  
  39. int main(int argc, const char * argv[])
  40. {
  41.  
  42.     std::cout << "result = " << mul_sh<2>(5);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement