Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Power of two #1

Closed
dasJ opened this issue Jul 9, 2013 · 1 comment
Closed

Power of two #1

dasJ opened this issue Jul 9, 2013 · 1 comment

Comments

@dasJ
Copy link

dasJ commented Jul 9, 2013

Hello,

this is not a bug or something like that, but I think, you can optimize your code.
https://github.com/lxndr/vtf/blob/master/vtf.cpp#L88
The method in this file isPowerOfTwo has a loop and can not recognize sizes > 32768.
The faster way is a simple macro:

define IS_POWER_OF_TWO(t) ( t > 0 && !(t & (t − 1)))

The simple reason for this is that powers of two look in binary like this:
1: 0001
2: 0010
4: 0100
8: 1000
Everytime, there is exacly one bit set.

  • Janne
@lxndr
Copy link
Owner

lxndr commented Jul 13, 2013

Thank you, dasJ! This seems to be the best way to do it.

@lxndr lxndr closed this as completed Jul 13, 2013
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants