Search code examples
c++exponential

Can you pre-compute a table of powers of 2 using templates


When I asked a question, that states what is the fastest way to find the powers of 2, I get the answer "precompute them and use a lookup table".

How can I pre-compute a table of powers of 2 using templates?

Link to my previous question: Better way to find the powers of 2


Solution

  • Here's an idea:

    constexpr std::array<unsigned long long, 16> LUT = {
         1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 };