- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
After all the WTF comments (the original article in itself isn't eally one) I will add another one people haven't mentioned yet. While the original test uses short-circuit evaluation, it would be far better to sort the test in the order likelyness. If most textures (assuming the code is used for that) are of size 128x128 then the check should be:
So there will be only one comparison in the most likely case.
Admin
whilst you're all very clever etc with your bitwise manipulation and things... what happens when the boss wants images to be supported in size 243x243 ?
Admin
Admin
Jesus.
It is
return (x <= 0) ? false : ( (x == 1) ? true : ((x & (x-1)) > 0) );
as x^0 = 1 for all x
and 0 and below are not possible powers for any positive integer
Admin
Whoops screwed that up... Eugh tying on a fucking ihone blows
Admin
The only WTF in this article are the people commenting on it and the submitter. This is pretty good embedded code written in C. There's been so much Java/SQL on here that people have clearly forgotten what C looks like.
Admin
On embebed systems it is not safe. Ok you also may not have a filesystem, but it is another story.
Admin
Other than a potential overuse of parenthesis, what's wrong with this?
return x==1 || (x&(x-1));
Addendum (2010-05-13 08:39): ^ oops, forgot the test against zero:
return x==1 || (x&(x-1))==0;
embarrased
Admin
powers of 10 interesting video if you havent seen it before
http://www.youtube.com/watch?v=AUUkjWsNC9k
Admin
The critical mistake many commenters here have made is assuming that the terse comment above the function is a full and correct specification of its desired behavior.
The function is named 'checkSize', not 'isPowerOfTwo' -- maybe there are other limitations on 'size' that are being considered?
If the actual requirement is that the function returns 1 if the argument is in the set [2,4,8,16,32,64,128,256,512], and 0 if it is any other value, then there isn't much that could be done to make the code more readable or efficient.
Admin
For C, can consider switch statements. When used correctly they can improve readability, many compilers optimize them, and they boost LOC for extra $$$.
Admin
2^0 = 1
Good job! Probably the biggest thing wrong with all of the commentors is they haven't taken a class where that mattered in the last 10 years.
Admin
The Real WTF is so many commenters confusing powers of 2 with even numbers.
(Yeah, and the original code actually works. It does not require extra math libraries. It needs no recursion. There is no need for looping for so few values to check, even if that would make it more maintainable (is that a word?).)
Admin
Admin
#define ISPOW2(x) ( x && !( (x) & ((x) - 1) ) )
Admin
B) You create 256x256 image and set the extra area to be transparent.
C) You find a better boss.
C is hard, but rewarding when successful.
Admin
I hope that's a joke... that algorithm is very broken as you'll find for any number that is not 4!
Admin
I think it's obvious from the original comment and function name that the function refers to image sizes, and there's some other reason an image could never be bigger than 512.
In that case I propose:
return x > 1 && (x & 1023) == x;
Admin
Just needs a regex. Duh...
:D
Admin
No, GCC does not produce code as fast as MSVC's code. It produces code which is approximately 0.1 times slower.
Admin
The real way to check for powers of two is to dynamically construct a database table at initialization with all the powers of two (can be easily done with a loop from 1 to sqrt(longmax)), and then just 'select * from table where num=&1' and see if that returns a line...
Admin
SWeet Jesus there are some piss poor answers in these comments.
Checking if it's even? Missing edge cases? C'mon...
(Also - I'm at uni using the open source tool chain, with all its pros and cons, and got a bollocking for using C99. Arghh...)
Admin
no comment
Admin
it's the pronunciation of "boolean" at the end that gets me :)
Admin
the winner!
Admin
For the record, it was Java-Code. :)
Thanks for posting, Daniel
Admin
Because Wikipedia is never wrong.</sarcasm>
Seriously, you look retarded when you quote Wikipedia. I know you think you look smart, but you don't. Nobody with half a brain takes Wikipedia seriously or those who use it.
Admin
I really like this part: while(checkVal<=INT_MAX)
Admin
I just got the joke. There needs to be a new classification on this site: FAF, or Funny As Fuck.
Admin
What's the power of ten that comes between 100,000,000 and 10,000,000,000?
Admin
How the hell can an image be power of 2???
Admin
Agree
Admin
Geez @comments. The comment in code says it tests for power of two, which it does not. The comments in this thread post incorrect code. There is one negative number for which the ! x&(x-1) test returns true.
The correct alternative is return x>1 && x<=512 && ! x&(x-1);/// powers of two from 2 to 512 inclusive.
Admin
Admin
then 1 & 0 IMHO equals 0, as long as we are using standard binary operators. Then the test works, QED.
Admin
0
Admin
Oh, I see..
Admin
Supra shoes are so popular all over the world. Whatever you take on Supra being in the Supra Skytop Shoes market. Now Supra Strapped Shoes also very popular attention. It is nice that they actually took the time to make Supra Skate Shoes that work well. Supra shoes is a brand that has been inspired, designed and marketed by passionate individuals. We have brought to you the fullest selection of Supra footwear at cheapest price.
Admin
Why are some people saying !(x&(x-1)) doesn't work? Of course it does (for positive numbers).
Actually I didn't check wikipedia and this was my first guess: !(~(x^(x-1)))
Admin
Hmm, although my solution above seemed good on paper, it doesn't work. The bitwise not won't do what I thought it would then. I still think this solution is easier to visualize on paper, so I came with a working (although a bit convoluted :P) solution:
~(x^(x-1)) == -(x<<1)
Admin
"Wrong" is wrong here, because
boolean isPow2(x) { return x & (x-1) == 0; }
is the correct answer. Why? Assume x = 10000, then x-1 = 1111. using the & as binary operator the solution would be 10000 & 1111 = 0 :)
Admin
I can't believe nobody has posted this yet... (assuming that the 2-to-512 constraint was a requirement)
Admin