Ten Ways to Check if an Integer Is a Power Of Two in C
exploringbinary.com
Ten Ways to Check if an Integer Is a Power Of Two in C
1–10 of 82 posts
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#2[deleted]
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#3[deleted]
It is method #9.
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#4Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#5[deleted]
Not only did you not bother to read the site, your version gives the wrong answer for x=0.
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#6Earlier quoted context omitted.
It is method #9.
[deleted]
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
#7Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#8One they missed: (x & -x) == x
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#9Earlier quoted context omitted.
[deleted]
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.
[deleted]
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#10One they missed: (x & -x) == x
Unfortunately this doesn't work for x=0.