• First Post (unregistered)

    It better should be:

    int checkSize(int x) {
      if (x == 2 || x == 4 || x == 8 || x == 16 || x == 32 || x == 64 || x == 128 || x == 256 || x == 512)
        return 1;
      else if ( x == 1 )
        return FRIST;
      else if ( x == 0 )
        return FILE_NOT_FOUND;
      else if ( -1 == x )
        return "Yoda here was";
      else
        return 0;
    }
    
  • Anonymous (unregistered)

    Another new author? Great! Welcome, John.

  • Ocson (unregistered)

    With regard to the lack of boolean, this could have been written in C.

  • Ed (unregistered)

    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

  • stephenb (unregistered)

    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?

  • (cs)

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

  • Bryan The K (unregistered)

    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

  • Jumble (unregistered)

    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.

  • (cs) in reply to Bryan The K
    Bryan The K:
    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

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

  • Anon (unregistered)

    Correct test for power of 2:

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

  • (cs)

    C has no boolean type.

  • (cs) in reply to Anon
    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".

    Wtf is wrong with all commentors.

  • Fred (unregistered)

    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

  • Bryan The K (unregistered) in reply to Evo

    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.

  • (cs) in reply to Ocson
    Ocson:
    With regard to the lack of boolean, this could have been written in C.
    Daid:
    C has no boolean type.

    // party like it's c99

    #include <stdbool.h>

  • AnOldHacker (unregistered)

    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.

  • JakeyC (unregistered)

    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.

  • Anon (unregistered)

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

  • Knux2 (unregistered)

    Psssh...not very Enterprise-y. This is how he SHOULD have written it (in Java):

    final static int TWO = 2;
    final static int FOUR = 4;
    final static int EIGHT = 8;
    final static int SIXTEEN = 16;
    final static int THIRTY_TWO = 32;
    final static int SIXTY_FOUR = 64;
    final static int ONE_TWENTY_EIGHT = 128;
    final static int TWO_FIFTY_SIX = 256;
    
    final static int[] POWERS_OF_TWO = {TWO, FOUR, EIGHT, SIXTEEN, THIRTY_TWO, SIXTY_FOUR, ONE_TWENTY_EIGHT, TWO_FIFTY_SIX};
    
    /*
    =============
    checkSize
    Make sure the image is a power of 2.
    Return -1 if x is too large.
    =============
    */
    int checkSize(int x) {
      if (java.util.Arrays.binarySearch(POWERS_OF_TWO, x) >= 0){
        return 1;
      } else if (x > POWERS_OF_TWO[POWERS_OF_TWO.length - 1])
        return -1;
      } else {
        return 0;
      }
    }
    
  • bl@h (unregistered)

    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; }

  • (cs)
    Most computer scientists could rattle off their powers of 2 just as easily as their powers of 10.

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

  • onaka (unregistered)

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

  • Bernard Mergendeiler (unregistered)

    Stopping at 512 might be a good idea. Wouldn't want the code to be two powerful.

  • Tyler (unregistered)

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

  • Anon (unregistered) in reply to onaka
    onaka:
    I guess checking x % 2 == 0 is clearer and slower but easy to forget if you're doing a quick hack ..

    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.

  • Helix (unregistered)

    TRWTF is putting an obvious pun in quotes.

  • Botia (unregistered)

    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.

  • Anon (unregistered) in reply to Tyler
    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);

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

  • Rune (unregistered)

    Judging from a lot of comments, it's been quite a while since people used bitwise comparisons...

  • x (unregistered)

    Are you serious!? He's fucking reading the code to us!?

  • (cs) 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".

    Wtf is wrong with all commentors.

    Well, the original WTF didn't get that part right, either.

  • Tyler (unregistered)

    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.

  • Thg (unregistered) in reply to Anon
    Anon:
    Tyler:
    Why not just... plain... return (x%2==0);

    Because it's wrong? x & (x-1) is correct. Check Wikipedia.

    bitshiftingsonufab**2

  • anon (unregistered) in reply to java.lang.Chris;
    java.lang.Chris;:
    Ocson:
    With regard to the lack of boolean, this could have been written in C.
    Daid:
    C has no boolean type.

    // party like it's c99

    #include <stdbool.h>

    Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?

  • pi (unregistered) in reply to Tyler
    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 = ?
  • Someone who can't be bothered to login from work (unregistered)

    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.

  • Me (unregistered) in reply to Bryan The K
    Bryan The K:
    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

    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.

  • Paster (unregistered) in reply to anon
    anon:
    Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?
    Some embedded systems might not have a cross-compiler that supports that yet, so for the sake of general portability of code, it's best to pretend it doesn't exist. It's really same as with filesystems, there might not always be one, so it's safest to assume there isn't.
  • Daniel (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".

    Wtf is wrong with all commentors.

    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.

  • (cs)

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

  • Thg (unregistered) in reply to First Post

    Less WTF than the WTF Less WTF than bitshifting

    int checkSize(int x) {
      int i, b;
      for ( b=0, i=1 ; i <= x && i != x; b++, i=1<
    
  • Sylver (unregistered)

    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.

  • Thg (unregistered) in reply to Thg

    I mean "masking"

  • Oxyd (unregistered)

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

  • Kef Schecter (unregistered) in reply to anon
    anon:
    Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?

    Because some compilers pretend it doesn't exist. Like, oh, Visual C++.

  • Ben (unregistered) in reply to Evo

    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.

  • heretic (unregistered) in reply to anon

    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.

  • @Deprecated (unregistered) in reply to Quicksilver
    Quicksilver:
    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..

    powerOfTwo(2) = ?

  • heretic (unregistered) in reply to anon
    anon:
    java.lang.Chris;:
    Ocson:
    With regard to the lack of boolean, this could have been written in C.
    Daid:
    C has no boolean type.

    // party like it's c99

    #include <stdbool.h>

    Seriously. C99 was ratified eleven years ago. Why do people pretend it doesn't exist?

    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.

  • @Deprecated (unregistered) in reply to @Deprecated
    @Deprecated:
    Quicksilver:
    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..

    powerOfTwo(2) = ?

    Ah, nevermind...

Leave a comment on “The Power of True”

Log In or post as a guest

Replying to comment #308544:

« Return to Article