- 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
It better should be:
Admin
Another new author? Great! Welcome, John.
Admin
With regard to the lack of boolean, this could have been written in C.
Admin
I cannot believe that some poor swear word had to sit and read that out.
I also cannot believe that I managed to get half way through the if statement before it became painful :D
Admin
am mostly liking the hear a blog but hearing someone say "returns i.n.t" just made me chuckle.I'm just not sure how someone reading code out loud works for this?
Admin
A nice (real) solution... int checkSize(int x) { return x == 1 || !(x & (x-1)); }
Edit: ah, one isn't counted, so this works: int checkSize(int x) { return !(x & (x-1)); }
Admin
int checkSize(int x) return Math.Sqrt(x) == 2; }
There - saved you a few lines of code. Wait, we're getting paid per line of code, aren't we?
CAPTCHA: minim - as in trying to get it done in the minim amount of work
Admin
int checkSize(int x) { return (x>1) && !(x&(x-1); }
Although I don't know why they don't count 1, but there you go.
Admin
Wait... What? I don't know java, but I assume it would need an opening {, and I would assume that Sqrt... takes the square root? Sqrt(64) != 2
Again, TRWTF are the comments. See my solution one post above yours ;-).
Admin
Correct test for power of 2:
return x & (x − 1)) == 0;
Admin
C has no boolean type.
Admin
Wrong. That would be "Correct test for power of 2's not equal to 1".
Wtf is wrong with all commentors.
Admin
Actually, this is good code for embedded systems where the total memory is only 512 bytes. Generic powers of 2 checking would still allow overflow
Admin
Wow, this is what happens when we don't drink coffee in the morning or have our morning cancer stick.
Disregard any further comments I ever post...
CAPTCHA: secundum. After posting incomplete code like that I needed to secundum a position in management.
Admin
// party like it's c99
#include <stdbool.h>
Admin
You guys are amazing. Suppose x = 0. What do you get for x & (x - 1), hmmm?
Always check your boundaries, people.
And 1 IS a power of two--I expect that its omission is yawtf.
Admin
Given he's passing an int, the possible set is no longer infinite and (assuming it's 32-bit signed) is limited to 2,147,483,647 - within which the highest possible power of 2 is 1,073,741,824.
The function isn't called 'isPowerOfTwo()', so the Int that gets returned could well be an id for, say, a lookup table.
TRWTF is not stating units. My image is 2048 big. Fact.
Admin
It could be that the maximum size allowed is 512 and hence why he stopped at 512. He could be well aware that there are powers of 2 above 512. The name of the function is, after all, "checkSize" which, without reading the comments, you might expect to check the max and min sizes as well (which this, implicitly, does). The only WTF is not knowing how to mathematically check for a power of 2 (and not using a bool as a return type).
Admin
Psssh...not very Enterprise-y. This is how he SHOULD have written it (in Java):
Admin
Man you guys post some serious overkill.
/// Make sure size of image is allowed /// All teh pics r belong us public static bool CheckSqrt(int x) { return true; }
Admin
Sure, if you let me recite them in terms of binary or hex. If not, I'll always find moving from "ten septillion" to "one hundred septillion" (25th to 26th power of 10) easier than "thirty-three million, five hundred fifty-four thousand, four hundred thirty-two" to "sixty-seven million, one hundred and eight thousand, eight hundred sixty-four."
Admin
This code is actually quite good in many cases as many people have already pointed out.
Using x & (x-1) is cool but not that easy to come up with unless you check wikipedia... also x & (x-1) really should have a comment or someone reading the code would not really know what it does ... I guess checking x % 2 == 0 is clearer and slower but easy to forget if you're doing a quick hack ..
Admin
Stopping at 512 might be a good idea. Wouldn't want the code to be two powerful.
Admin
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);
Admin
And also wrong. x % 2 == 0 is a good way to check if a number is even, not if it's a power of 2.
Admin
TRWTF is putting an obvious pun in quotes.
Admin
If this is from some older texture code, there was probably an upper limit of 512x512 for the size of the texture as well. While there is a more elegant and perhaps slightly faster method, it appears that all is missing is some comments describing exactly what the function should be used for. Perhaps that can be seen from the context of the entire source.
Admin
Because it's wrong?
x & (x-1) is correct. Check Wikipedia.
But since I wouldn't have pulled that off the top of my head, I think the solution in the original post is probably acceptable and will make little difference in performance (assuming check size isn't called a lot).
Admin
Judging from a lot of comments, it's been quite a while since people used bitwise comparisons...
Admin
Are you serious!? He's fucking reading the code to us!?
Admin
Admin
Apologies, there's a TRWTF. Shows I've been up too long to post an answer that accepts x==0, as for the comment x&(x-1))==0 being incorrect, I mis-parsed it mentally. I discarded extraneous paren then parsed x&(x-1)==0 which gives for 2: 2&(2-1)==0, 2&1==0, 2&0, 0, false.
Admin
bitshiftingsonufab**2
Admin
Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?
Admin
Admin
If you were working with an unsigned int you could do
bool checkSize(unsigned int x) { return x == (x&-x); }
Assuming my brain isn't entirely fried, of course.
Admin
I'll assume you're serious. You just checking if x is 4, and even if you meant to use log2(x) ... or whatever C#'s log base 2 function is ... it's considerably slower than return x & (x-1) == 0
Guys, this is a standard interview question. Get it right.
Admin
Admin
I must be missing something here. 1 & 0 == 0, that's absolutely correct. No special-casing is needed for 1, unlike what you and a few others said.
Unless you mean that the desired outcome (given the original code) is not to include 1. In which case I believe your wording is incorrect.
Admin
One more power of 2 check:
boolean powerOfTwo(int n) { if (n <= 0) { return false; } if (n % 2 == 1) { return n == 1; } else { return powerOfTwo(n/2); } }
no need to be clever when computers are fast and your compiler knows how to do tailrecursion stackfriendly..
Admin
Less WTF than the WTF Less WTF than bitshifting
Admin
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.
Admin
I mean "masking"
Admin
Frankly, I don't see the WTF in there. The code enumerates a -- fairly small -- set of correct sizes. If the whole disjunction contained hundreds or thousands of literals, it would be worth a lul.
Also, this solution is not as cryptic as the mentioned bithacks. Not to mention that your bithacks are mostly wrong: the parameter is of type int, not unsigned int. -2147483648 is not a natural power of two, as far as I know. And even here I'm assuming 32-bit ints in two's complement. Now consider an embedded system without a harddisk using sign-and-magnitude signed integer representation..
Admin
Because some compilers pretend it doesn't exist. Like, oh, Visual C++.
Admin
Er, 2^0=1, so 1 is a power of two, so you're the wtf. The problem with this code is that it should return false for numbers <= 0.
I would thus argue for:
return ((x > 0) ? ((x & (x-1)) == 0) : 0);
So there.
Admin
Because Microsoft support for the C99 standard is less than stellar?
For all of Linux's distinct advantages (open source, GCC, etc.) as a development platform, I know of precious few businesses actually doing development on it (and even fewer universities which teach using the open source development tool-chain).
Remember it is all about vendor lock-in.
Admin
powerOfTwo(2) = ?
Admin
Because Microsoft support for the C99 standard is less than stellar?
For all of Linux's distinct advantages (open source, GCC, etc.) as a development platform, I know of precious few businesses actually doing development on it (and even fewer universities which teach using the open source development tool-chain).
Remember it is all about vendor lock-in.
Admin
Ah, nevermind...