• gallier2 (unregistered)

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:

```int checkSize(int x) {
return (x == 128 || x == 64 || x == 256 || x == 32 || x == 16 || x == 8 || x == 4 || x == 2 || x == 512);
}
```

So there will be only one comparison in the most likely case.

• Josephus (unregistered)

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 ?

• (cs) in reply to Josephus
Josephus:
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 ?
`#define 243 256`
• eViLegion (unregistered)

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

• eViLegion (unregistered) in reply to eViLegion

Whoops screwed that up... Eugh tying on a fucking ihone blows

• C (unregistered)

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.

• (cs) in reply to Buddy
Buddy:
Shriike:
1 == 1 & -1 (001 == 001 & 111) 001 == 001 = true 2 == 2 & -2 (010 == 010 & 110) 010 == 010 = true

What's the problem?

catpch nisl: That just sounds awesome ;)

C doesn't specify two's complement representation for signed integers. But like assuming A to Z are contiguous, most of the time it's safe.

On embebed systems it is not safe. Ok you also may not have a filesystem, but it is another story.

• (cs)

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

• powers of 10 (unregistered)

powers of 10 interesting video if you havent seen it before

• (cs)

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.

• Buddy (unregistered) in reply to Rootbeer
Rootbeer:
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.

For C, can consider switch statements. When used correctly they can improve readability, many compilers optimize them, and they boost LOC for extra \$\$\$.

```/*
As per specs, big boss man says legal sizes are
currently powers of 2 between 2 and 512 inclusive.
Big boss man says any deviation means heads will
roll! I listen to big boss man.
*/
int checkSize(int x)
{
switch (x)
{
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
return 1;

default:
return 0;
}
}
```
• Andrew (unregistered) in reply to Evo

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.

• (cs)

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?).)

• illtiz (unregistered) in reply to anon
anon:
Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?
Because on an embedded platform with no filesystem...
• Wolfgang Draxinger (unregistered)

#define ISPOW2(x) ( x && !( (x) & ((x) - 1) ) )

• Paster (unregistered) in reply to Josephus
Josephus:
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 ?
A) You tell the boss to talk to the chip maker to support such image size, not only power-of-two sizes.

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.

• FooWarrior (unregistered) in reply to Bryan The K

I hope that's a joke... that algorithm is very broken as you'll find for any number that is not 4!

• Steve Bennett (unregistered)

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;

• anon (unregistered)

Just needs a regex. Duh...

```boolean isPowerOfTwo(int i) {
return Pattern.matches("^0*10*\$", Integer.toBinaryString(i));
}
```

:D

• fribo (unregistered) in reply to Jimmy Jones
Jimmy Jones:
GCC produces code which is as small/fast as the Microsoft compiler?

No, GCC does not produce code as fast as MSVC's code. It produces code which is approximately 0.1 times slower.

• Woo (unregistered)

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...

• codinghands (unregistered)

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...)

• Fredita (unregistered) in reply to Evo
Evo:
Wtf is wrong with all commentors.

no comment

• spamspamspamspam (unregistered)

it's the pronunciation of "boolean" at the end that gets me :)

• Thg (unregistered) in reply to Buddy
Buddy:

For C, can consider switch statements. When used correctly they can improve readability, many compilers optimize them, and they boost LOC for extra \$\$\$.

```/*
As per specs, big boss man says legal sizes are
currently powers of 2 between 2 and 512 inclusive.
Big boss man says any deviation means heads will
roll! I listen to big boss man.
*/
int checkSize(int x)
{
switch (x)
{
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
return 1;

default:
return 0;
}
}
```

the winner!

• Daniel (unregistered)

For the record, it was Java-Code. :)

Thanks for posting, Daniel

• Anonyomous (unregistered) in reply to Thg

x & (x-1) is correct. Check Wikipedia.

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.

• qbolec (unregistered) in reply to frits
frits:
```int checkSize(int x)
{
int checkVal =0;
int i = 0;
//BTW-Powers of two are not an infinite set in real systems
while(checkVal<=INT_MAX)
{
checkVal = 1<<i++;
if(checkval==n)
{
return 1;
}
}
return 0;
}
```

BTW- "Hear A Blog" is pure win. Please keep this feature.

I really like this part: while(checkVal<=INT_MAX)

• (cs) in reply to Bernard Mergendeiler

I just got the joke. There needs to be a new classification on this site: FAF, or Funny As Fuck.

• (cs)

What's the power of ten that comes between 100,000,000 and 10,000,000,000?

• Jacek B (unregistered)

How the hell can an image be power of 2???

• Aleks (unregistered) in reply to Sylver
Sylver:
TRWTF here is that this code ends up on thewailywtf.com in the first place.

It's a short piece of code that works. It's not elegant, bordering on clumsy, but hey, it works, and it is easy to understand. That's more than can be said of most of the answers given in the comments.

How about some real WTF? Gosh, I miss the brillant days of Paula.

Agree

• jerk_programmer (unregistered)

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.

• qbolec (unregistered) in reply to jerk_programmer
jerk_programmer:

The correct alternative is return x>1 && x<=512 && ! x&(x-1);/// powers of two from 2 to 512 inclusive.

• ~~ (unregistered) in reply to Evo
Evo:
Anon:
Correct test for power of 2:

return x & (x − 1)) == 0;

Wrong. That would be "Correct test for power of 2's not equal to 1".

Really? OK, let's check: x equals 1 x-1 equals? IMHO it'd be 0...

then 1 & 0 IMHO equals 0, as long as we are using standard binary operators. Then the test works, QED.

• Goatie (unregistered) in reply to pi
pi:
Tyler:
Did I miss something or is today prank answer day? NONE of the answers above are correct including x&(x-1) w/ typo extraneous ')'. Why not just... plain... return (x%2==0);
6 % 2 = ?

0

• Goatie (unregistered) in reply to Goatie
Goatie:
pi:
Tyler:
Did I miss something or is today prank answer day? NONE of the answers above are correct including x&(x-1) w/ typo extraneous ')'. Why not just... plain... return (x%2==0);
6 % 2 = ?

0

Oh, I see..

• oksupra.com (unregistered)

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.

• Kemosabe-TBC (unregistered)

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)))

• Kemosabe-TBC (unregistered) in reply to Kemosabe-TBC

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)

• Anon (unregistered) in reply to Evo

"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 :)

• anonymous (unregistered)

I can't believe nobody has posted this yet... (assuming that the 2-to-512 constraint was a requirement)

```/*
=============
checkSize
Make sure the size is a power of 2 in the range 2 to 512.
=============
*/
int checkSize(int x) {
return (x & !((512 << 1) - 2) | x & x >> 1 | x & x >> 2 | x & x >> 3
| x & x >> 4 | x & x >> 5 | x & x >> 6 | x & x >> 7 | x & x >> 8
| x & x >> 9 | x & x >> 10 | x & x >> 11 | x & x >> 12 | x & x >> 13
| x & x >> 14 | x & x >> 15 | x & x >> 16 | x & x >> 17 | x & x >> 18
| x & x >> 19 | x & x >> 20 | x & x >> 21 | x & x >> 22 | x & x >> 23
| x & x >> 24 | x & x >> 25 | x & x >> 26 | x & x >> 27 | x & x >> 28
| x & x >> 29 | x & x >> 30 | x & x >> 31) == 0;
}```
• anonymous (unregistered) in reply to anonymous
anonymous:
I can't believe nobody has posted this yet... (assuming that the 2-to-512 constraint was a requirement)
```/*
=============
checkSize
Make sure the size is a power of 2 in the range 2 to 512.
=============
*/
int checkSize(int x) {
return (x & !((512 << 1) - 2) | x & x >> 1 | x & x >> 2 | x & x >> 3
| x & x >> 4 | x & x >> 5 | x & x >> 6 | x & x >> 7 | x & x >> 8
| x & x >> 9 | x & x >> 10 | x & x >> 11 | x & x >> 12 | x & x >> 13
| x & x >> 14 | x & x >> 15 | x & x >> 16 | x & x >> 17 | x & x >> 18
| x & x >> 19 | x & x >> 20 | x & x >> 21 | x & x >> 22 | x & x >> 23
| x & x >> 24 | x & x >> 25 | x & x >> 26 | x & x >> 27 | x & x >> 28
| x & x >> 29 | x & x >> 30 | x & x >> 31) == 0;
}```
Crap, I forgot to add the check for x == 0.