I am new to python. Is 1 << n
is always better than 2 ** n
? Why or Why not?
Are there any better ways to find the powers of 2?
An important programming tenet: don't over-optimise prematurely.
Given that 1 << n
will be performed in integer arithmetic which significantly limits the results space, the fastest way would be to precompute them and use a lookup table (e.g. a simple array). For a 64 bit unsigned integral types such a table would only have 64 entries.
(In C++ you could even evaluate them at compile time with a bit of template trickery.)