• Oxyd (unregistered) in reply to heretic
    heretic:
    For all of Linux's distinct advantages (open source, GCC, etc.)

    GCC is an advantage? I thought it was a steaming pile of crap.

    I'm totally looking forward to seeing LLVM/Clang completed and hope that everyone will abadon GCC the moment it's released.

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

    Because I've been corrected properly (I believed it didn't work for 1, but of course it was 0 and less obviously), and have nothing intelligent to say all I can say is... F*ck you.

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

    Because it still doesn't exist in some problem spaces. Like certain compilers for embedded processors. sigh Sometimes you feel lucky if the compiler even accepts //-style comments.

  • Thg (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..

    I dig on the recursion, but ... uh ... exactly what types does your function accept and return ?

  • Ouch! (unregistered) in reply to Sylver
    Sylver:
    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.
    Yes. But let's be honest, that's what we love about this site, people calling other people retards for not knowing how to X - and then screwing up far worse. In public. Isn't it?
  • Neville Flynn (unregistered)

    So is there going to be a daily WTF article about a certain website that thought it'd be a good idea to have audio-narrated code?

  • Anonymouse (unregistered) in reply to Oxyd
    Oxyd:
    Now consider an embedded system without a harddisk using sign-and-magnitude signed integer representation..

    I would need to double check the exact wording in the specs, but the semantics of C bitwise operators are defined in terms of two's complement, regardless of the underlying architecture.

  • Someone who can't be bothered to login from work (unregistered) in reply to Kef Schecter
    Kef Schecter:
    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++.

    I suspect Visual C++ is never going to support C99 either; the level of customer demand for it is probably so low MS can safely ignore it. I think it supports C89/C90 and that's about it.

    I'd actually be kind of curious to know how many people are using Visual C++ to write C.

  • Ouch! (unregistered) in reply to Thg
    Thg:
    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..

    I dig on the recursion, but ... uh ... exactly what types does your function accept and return ?

  • Herby (unregistered) in reply to anon

    [quote user="anon"] // party like it's c99

    #include <stdbool.h> [/quote]

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

    Maybe so, but K&R haven't written a third edition of their book describing it. So, maybe it doesn't exist!

  • Dave-Sir (unregistered) in reply to Someone who can't be bothered to login from work
    Someone who can't be bothered to login from work:
    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.

    sizzle

    Your function will return TRUE only if x==0.

    Captcha "incassum": incassum wrong, I plead the fifth.

  • Paula (unregistered)

    1.) Check Size 2.) ????? 3.) Profit!

  • Thg (unregistered) in reply to Ouch!
    Ouch!:
    Thg:
    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..

    I dig on the recursion, but ... uh ... exactly what types does your function accept and return ?

    <ahem>

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

    Well, the standard exists, but no compilers do.

    Captcha "modo": Quasi-state

  • pflock (unregistered) in reply to Oxyd
    Oxyd:
    heretic:
    For all of Linux's distinct advantages (open source, GCC, etc.)

    GCC is an advantage? I thought it was a steaming pile of crap.

    I'm totally looking forward to seeing LLVM/Clang completed

    So, until Clang is completed, which of the available compilers are better w.r.t. optimization?

    ICC? Maybe, but it is playing unfair on non-Intel CPUs, and it doesn't run on non x86/64 CPUs. MSVC? Lol. They don't even use expression templates for valarray in their stdlibc++. And it doesn't run on non x86/64 CPUs. And their inline assembler is crap and non-existant on x64. The only thing it is good for is debugging, but then comes Nokia again who have quite mature gdb-integration.

    and hope that everyone will abadon GCC the moment it's released.
    So that there won't be competition anymore. Applause for oxyd.

    oh why can't I stop posting replies to obviously ignorant trolls

  • Shriike (unregistered) in reply to Dave-Sir
    Dave-Sir:
    Someone who can't be bothered to login from work:
    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.

    sizzle

    Your function will return TRUE only if x==0.

    Captcha "incassum": incassum wrong, I plead the fifth.

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

  • Shriike (unregistered) in reply to Thg
    Thg:
    <ahem />
    FTFY

    captch dignissim : an obsession with digg.com

  • Ouch! (unregistered) in reply to Thg
    Thg:
    Ouch!:
    Thg:
    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..

    I dig on the recursion, but ... uh ... exactly what types does your function accept and return ?

    <ahem>
    Yes? false is one of two values a boolean can have (the other is true). A comparison x == y returns a [color=#802060]. Everything's correct. Or is it that you didn't see this was Java?

  • Ouch! (unregistered) in reply to Thg
    Thg:
    Ouch!:
    Thg:
    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..

    I dig on the recursion, but ... uh ... exactly what types does your function accept and return ?

    <ahem>
    Yes? false is one of two values a boolean can have (the other is true). A comparison x == y returns a boolean. Everything's correct. Or is it that you didn't see this was Java?

  • (cs)
    
    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;
    }
    
    </pre>
    

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

  • (cs)

    Even though this is not the most efficient solution (by any stretch of the imagination), I've always been a sucker for recursion and bitwise operators:

    public static boolean isPowerOfTwo( int i )
    {
            return i == 1 || ( i != 0 && ( i & 1 ) == 0 && isPowerOfTwo( i >> 1 ) );
    }
  • Thg (unregistered) in reply to Ouch!
    Ouch!:
    Yes? false is one of two values a boolean can have (the other is true). A comparison x == y returns a boolean. Everything's correct. Or is it that you didn't see this was Java?

    arggh! You are correct.
    I concede as gracefully as possible ( given that I was all trying to point out mistakes that weren't actually there. )

    My brain was working in C.

    (what? did nobody like the for-loop/bitshift solution?)

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

  • Anonymous (unregistered)
    Yesterday's Article:
    Obviously, not all of the content on The Daily WTF is appropriate for narration (Code SOD, Error’d, etc), so the feed only contains articles worthwhile listening to.
    Is it a meta-WTF that you've narrated a CodeSOD?
  • TheRealMe (unregistered)

    Speaking of gcc, I got to learn about fun gcc quirks recently. sigh Apparently there are some subtle floating-point handling issues - which is unfortunate, as code I use uses floats to some extent (and in some cases, doubles).

  • Steve The Cynic (unregistered) in reply to Someone who can't be bothered to login from work
    Someone who can't be bothered to login from work:
    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.

    There is a faint sizzling sound:

    • It returns true for x == 0 ( 0 & (n'import quel valeur) is equal to 0 )
    • It negates an unsigned value, leading to the next point
    • It returns false for powers of two on ones-complement architectures ( i.e. where -x == ~x )

    Aside from that it's not bad.

  • Steve The Cynic (unregistered) in reply to Dave-Sir
    Dave-Sir:
    Someone who can't be bothered to login from work:
    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.

    sizzle

    Your function will return TRUE only if x==0.

    Captcha "incassum": incassum wrong, I plead the fifth.

    You are wrong, you'd better plead now.

    four-bit word:

    Try x == 1 -x == -1 == 0000 - 0001 == 1111 x & -x == 0001 & 1111 == 0001 == x TRUE!

    Try x == 2 -x == -2 == 0000 - 0010 == 1110 x & -x == 0010 & 1110 == 0010 == x TRUE!

    Try x == 3 -x == -3 == 0000 - 0011 == 1101 x & -x == 0011 & 1101 == 0001 != x FALSE!

    etc.

    But it is still broken if x == 0.

    Watch out for that "only if". "X only if Y" is equivalent to "if X then Y". You probably meant "returns TRUE ** if x == 0" <- no "only" at the **

  • AnOldRelic (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?

    Change? You want us to change? Really? Never. C is C is C, no changes allowed! C99exist=0;

  • fjf (unregistered) in reply to Paster
    Paster:
    Some systems might not have a compiler, so for the sake of general portability of code, it's best to pretend it doesn't exist. It's really same as with disks, network, graphics or text I/O, there might not always be one, so it's safest to assume there isn't.
    FTFY.
  • fjf (unregistered) in reply to Knux2
    Knux2:
    Psssh...not very Enterprise-y. This is how he SHOULD have written it (in Java): [...]
    No XML. FAIL.
  • (cs)

    The accent still gives me a headache, but the audio was brilliant!

    Return i-n-t?

    However it's a bit difficult to understand from the audio what the code actually was doing - why did they omit this bit: /*

    ============= */

    and the braces?

    Maybe the narrator could also learn to colour-code the text for clarity?

  • Karl (unregistered)

    This must be the saddest comment thread EVER.

    The code mode likely does what it should albeit not really clever nor named properly, as opposed from 99% of the "corrections" among the comments.

    Now, I'll wait for the next salmiyuck-post instead or, go look at the next wft-question from some lame indian coder on stackoverflow instead. Show me the codez... dude.

  • wds (unregistered)

    TRWTF is people who think

    x & (x-1)
    is cryptic. They really do hand out degrees to anyone these days.

  • Someone who can't be bothered to login from work (unregistered) in reply to Buddy
    Buddy:
    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.

    That is very true :)

  • Ouch! (unregistered) in reply to Thg
    Thg:
    My brain was working in C.
    That's what I suspected. Although, if you replace boolean with bool (or
    #define boolean bool
    ) and
    #include <stdbool.h>
    , it's also valid C.
    (what? did nobody like the for-loop/bitshift solution?)
    Well, I did, except for a few minor details, but not enough to post. But since we're now on that topic,
    for ( b=0, i=1 ; i <= x && i != x; b++, i=1<
    What have you against 
    i < x
    ? And anyway, I'd prefer
    for(i = 1; i < x; i <<= 1);
    
    But it needs a test
    x <= (INT_MAX >> 1) + 1
    before, or you'll enter undefined-behaviour-land (in practice, you can be almost certain that it's an infinite loop).
  • Someone who can't be bothered to login from work (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    Someone who can't be bothered to login from work:
    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.

    There is a faint sizzling sound:

    • It returns true for x == 0 ( 0 & (n'import quel valeur) is equal to 0 )
    • It negates an unsigned value, leading to the next point
    • It returns false for powers of two on ones-complement architectures ( i.e. where -x == ~x )

    Aside from that it's not bad.

    It was actually written with the assumption that your arch did two's compliment - in fact it relies on it.

    The first one is a bug, of course.

  • Thg (unregistered) in reply to Ouch!
    Ouch!:
    (what? did nobody like the for-loop/bitshift solution?)
    Well, I did, except for a few minor details, but not enough to post. But since we're now on that topic,
    for ( b=0, i=1 ; i <= x && i != x; b++, i=1<
    What have you against 
    i < x
    ? And anyway, I'd prefer
    for(i = 1; i < x; i <<= 1);
    
    But it needs a test
    x <= (INT_MAX >> 1) + 1
    before, or you'll enter undefined-behaviour-land (in practice, you can be almost certain that it's an infinite loop).

    well, like most WTFs it was cruft from the iterative process of creation. ;-)

    as far as the check, I assume that x already has to be less-than INT_MAX, so ....

    totally dignissim (captcha) the

    for(i = 1; i < x; i <<= 1);
    
  • RandomUser423686 (unregistered) in reply to Markp
    Markp:
    ... "sixty-seven million, one hundred and eight thousand, eight hundred sixty-four."
    The context made it more than clear what was meant, but did anyone else read this the intended way first, and then cynically as 67000100.8864?

    Or was it just my elementary school teachers who got all strict about "you only say 'and' in a number where you would put the decimal separator, and your answer is wrong if you mess it up," way back when?

  • Steve H (unregistered)

    Obviously not a WTF. Clearly they don't have images in infinitely large sizes. Clearly it's C, which lacks a boolean type. Clearly it's not clear that he wants a boolean anyway.

  • (cs) in reply to AnOldHacker

    Two to the power negative infinity is zero.

  • Ouch! (unregistered) in reply to Thg
    Thg:
    Ouch!:
    But it needs a test
    x <= (INT_MAX >> 1) + 1
    before, or you'll enter undefined-behaviour-land (in practice, you can be almost certain that it's an infinite loop).

    well, like most WTFs it was cruft from the iterative process of creation. ;-)

    That's okay, then. I assume you'd have seen and corrected it on going over the code before submitting to source control.
    as far as the check, I assume that x already has to be less-than INT_MAX, so ....
    But (assuming two's complement of course) if
    2^(INT_BITS - 2) < x <= INT_MAX
    , after
    i == 2^(INT_BITS - 2)
    , you get a value not representable in int, so undefined behaviour. I'd be surprised if any implementation gave something other than INT_MIN then, but it could do anything.
  • (cs)

    Half of these comments make my eyes bleed.

  • (cs)

    These comments prove beyond a shadow of a doubt that 9 out of 10 TDWTF readers should never be allowed near a compiler... or a computer at all.

    What happened to the readership of this site? I used to be able to say that at least most people here could write better code than the WTFs themselves. Now it's like a bunch of beer-gutted tube jockeys snickering and swearing at professional athletes.

  • Ouch! (unregistered) in reply to RandomUser423686
    RandomUser423686:
    Or was it just my elementary school teachers who got all strict about "you only say 'and' in a number where you would put the decimal separator, and your answer is wrong if you mess it up," way back when?
    No, that seems to be rather common in the USA. So much so that many (a few handful at least) merrickuns can't cope with the fact that the rest of the English speaking world has different conventions.
  • (cs)

    I can't really do much apart from gape my mouth and shake my head...

    At YOU people. My GOD, you people are insane. Apparently, explicitness is a bad thing now?

  • (cs)

    Tsk, tsk. Think of the performance hit of all those comparisons! Obviously the best way to implement this function is to use a precomputed lookup table:

    extern int g_is_power_of_two[];
    
    int checkSize(int x) {
        return (x >= 0) ? g_is_power_of_two[x] : 0;
    }
    
    int g_is_power_of_two[] = {
        1, // 0 is 2**(-Infinity)
        1, // 1 is 2**0
        1, // 2 is 2**1
        0,
        1, // 4 is 2**2
        0, 
        0,
        0,
        1, // 8 is 2**3
        0, 
        ...
    };
    
  • regeya (unregistered) in reply to Anon
    Because it's wrong?

    x & (x-1) is correct.

    That's what I was thinking, too, as long as x > 0.

    I don't know what's wrong with the whippersnappers.

  • (cs) in reply to Ouch!
    Ouch!:
    i == 2^(INT_BITS - 2)
    , you get a value not representable in int, so undefined behaviour. I'd be surprised if any implementation gave something other than INT_MIN then, but it could do anything.
    Uh,
    i == 2^(INT_BITS - 2);
    = 0x1C in a 32 bit system. That's hardly undefined.
  • entropy (unregistered) in reply to anon

    Because MSVC still doesn't implement it (they don't even implement C89, although it's a lot closer), so us poor bastards forced to do multi-platform, multi-toolchain development aren't able to use it.

  • Ouch! (unregistered) in reply to frits
    frits:
    Ouch!:
    i == 2^(INT_BITS - 2)
    , you get a value not representable in int, so undefined behaviour. I'd be surprised if any implementation gave something other than INT_MIN then, but it could do anything.
    Uh,
    i == 2^(INT_BITS - 2);
    = 0x1C in a 32 bit system. That's hardly undefined.
    Firstly, after, not when. Secondly, I used (^) as the exponentiation operator, of course (and anyway, you don't know how INT_BITS is defined, so it might well be undefined behaviour even if I had used (^) as `xor`).

Leave a comment on “The Power of True”

Log In or post as a guest

Replying to comment #:

« Return to Article