Your isPowerOfTwo-Function can be even more simplified:
isPowerOfTwo
template <typename T> bool IsPow2(T x) { return (x & 1) == 0; }
That's because any positive binary number that is a power of two doesn't have the bit representing '1' set.
Otherwise, thanks for the nice post.
This isn't correct. Your code checks if the number is even - 0, 2, 4, 6, 8, 10, 12, 14, ... That's not the same as power of two, which is 1, 2, 4, 8, 16, 32, 64, ...
Ah, that's true, I missed that.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your
isPowerOfTwo
-Function can be even more simplified:That's because any positive binary number that is a power of two doesn't have the bit representing '1' set.
Otherwise, thanks for the nice post.
This isn't correct. Your code checks if the number is even - 0, 2, 4, 6, 8, 10, 12, 14, ... That's not the same as power of two, which is 1, 2, 4, 8, 16, 32, 64, ...
Ah, that's true, I missed that.