Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by phoyd on Tuesday July 26, 2011 @10:01AM
(#2807333)
It is method #9.
[ Parent
| on HN ]
[deleted]
Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by Jabbles on Tuesday July 26, 2011 @10:13AM
(#2807382)
Because the methods clearly get more complicated. Starting simply and naively and then improving is a perfectly standard way of teaching.
Articles like these, which provide detailed explanation of the methods involved and results of benchmarks help greatly.
Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by esrauch on Tuesday July 26, 2011 @10:44AM
(#2807553)
The benchmarks would probably be somewhat misleading in a lot of cases here. I'm fairly certain that most compilers will replace a /2 with >>1 for example, so that isn't really going to end up being reflected when you compile it.
Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by feb on Tuesday July 26, 2011 @11:21AM
(#2807729)
That depends on the CPU used. Modern CPU have more arithmetic units than logical units and can perform more divisions than bit shifts. There was a good video presentation of that, but
I can't find the link now.
EDIT: http://blogs.msdn.com/b/shawnhar/archive/2007/03/19/a-story-... gives other reasons why multiplications can be faster than bit shifts.