In the world of bits...



  • This method was found in C++ production code in the company I work for.



    Can you spot the WTF ?

    int MyClass::GetTag(int value)
    {

    int tag = 0;
    double res = 0;

    for ( int index = 0; index <= 7; index++ )
    {
    if ( (value & 0x1) && (index < 5) )
    {
    res = pow(2, index);
    tag += (int)res;
    }
    value >>= 1;
    }

    return tag;
    }

    You'll be glad to hear that it has since been fixed, although  no one has yet owned up to the original code :P



  • @littleram said:

    Can you spot the WTF ?


     - The method's not inline
     - Usage of >>= 1 instead of *= 0.5 ?
     - Usage of an integer loop index
     - (value & 0x1) should be replaced with (value - ( value / 2 * 2 ))
     - && should be replaced with *

    But, overall, a much more elegant solution would be to convert value into an ascii binary number, get the last four characters, and convert it back to integer.



  • Correct me if I'm wrong, but couldn't this be replaced with:

    tag = value & 0x1f;

    ??



  • Of course, it can. It is kind of a tradition on the daily wtf to figure out how to "improve" the WTFness of the code.
    By the way, my string based suggestion was wrong, was 5 digits that you must keep, not 4.



  • Yep, that's it..

    tag = value & 0x1F;



    :D



    • for loop should go to MAX_INT
      - "value >>= 1" should be "value = (value & 0x1e) / 2"
      - tag should be a pointer to a float
      - for loop should include additional, unused loop var.
      - "double res" should be "register double res"



  • @littleram said:

    This method was found in C++ production code in the company I work for.



    Can you spot the WTF ?

    int MyClass::GetTag(int value)
    {

    int tag = 0;
    double res = 0;

    for ( int index = 0; index <= 7; index++ )
    {
    if ( (value & 0x1) && (index < 5) )
    {
    res = pow(2, index);
    tag += (int)res;
    }
    value >>= 1;
    }

    return tag;
    }

    You'll be glad to hear that it has since been fixed, although  no one has yet owned up to the original code :P




    Wow, the whole thing's a WTF...


    1. if ( (value & 0x1) && (index < 5) )  -- why not just use index < 5 in the for loop?
    2. why is res a double? 2^x is always going to be an int
    3. as others have said, doesnt this just do value & 0x1F?


  • @Zlodo said:

    - The method's not inline




    Functions larger than a few lines should very rarely be inlined. The
    gain in speed is immeasurably small unless the function gets called
    massive numbers of times, and inlining too much actually makes the
    program slower because less of it fits in cache.



    If you simplified the function down to "return value & 0x1f", then it would be small enough to make inlining sensible.



  • Inlining basically only makes sense if the function is extremely small, or called only from one (or maybe two) places.  In either of those cases, the compiler will probably do the job for you.  Manually specifying such things is generally a waste of time when evidence to the contrary is lacking.



  • @littleram said:

    This method was found in C++ production code in the company I work for.

    Can you spot the WTF ?
    ......
     although  no one has yet owned up to the original code :P

    The WTF is that your change control process does not appear to record who makes changes.



  • @Goplat said:

    @Zlodo said:
    - The method's not inline


    Functions larger than a few lines should very rarely be inlined.


    The real WTF is the number of people with a broken sarcasm detector on these forums.



  • @Zlodo said:

    @Goplat said:
    @Zlodo said:
    - The method's not inline


    Functions larger than a few lines should very rarely be inlined.


    The real WTF is the number of people with a broken sarcasm detector on these forums.


    The real WTF is the number of people with an obfuscated sense of humour.



  • @LoztInSpace said:

    @littleram said:

    This method was found in C++ production code in the company I work for.

    Can you spot the WTF ?
    ......
     although  no one has yet owned up to the original code :P

    The WTF is that your change control process does not appear to record who makes changes.

    Indeed. There's a reason Subversion has a command called "svn blame"...



  • @makomk said:

    @LoztInSpace said:

    @littleram said:

    This method was found in C++ production code in the company I work for.

    Can you spot the WTF ?
    ......
     although  no one has yet owned up to the original code :P

    The WTF is that your change control process does not appear to record who makes changes.

    Indeed. There's a reason Subversion has a command called "svn blame"...





    Blame is the best function of all in svn. ;)



  • @Ulvhamne said:

    @makomk said:
    @LoztInSpace said:

    @littleram said:

    This method was found in C++ production code in the company I work for.

    Can you spot the WTF ?
    ......
     although  no one has yet owned up to the original code :P

    The WTF is that your change control process does not appear to record who makes changes.

    Indeed. There's a reason Subversion has a command called "svn blame"...





    Blame is the best function of all in svn. ;)

    there's also praise iirc which does the same thing


Log in to reply