Flags: The easy way



  • What this code does is determine which bits in a byte are set, but it does so in an amazingly ... peculiar way.

    XString codeStr = roster->GetCode();
    int codeInt = 0;
    char code;

    try
    {
      try
      {
        codeInt = atoi(codeStr.data());
        code =  (char) codeInt;
      }
      catch(...)
      {
        XString er = "fout";
        throw er;
      }
    }
    catch(XString)
    {
      code = codeStr(0);
    }
    catch(...)
    {
      code = (char) 0;
    }

    char flag1, flag2, flag3, flag4,
         flag5, flag6, flag7, flag8;

    bool * codeBit = new bool[8];
    for(int b = 0; b < 8; b++)
    {
      int v = 1;
      for(int m = 0; m < b; m++)
      {
        v = v * 2;
      }
      char mask = (char) v;
      char maskbit = (char) (code & mask);
      if(maskbit != 0)
        codeBit[b] = true;
      else
        codeBit[b] = false;
    }

    if(someCondition)
    {
      if(codeBit[1])
        flag1 = '1';
      else
        flag1 = '0';
      //... snip

    }
    else
    {
      if(codeBit[1])
        flag1 = '1';
      else
        flag1 = '0';
      if(codeBit[0])
        flag2 = '1';
      else
        flag2 = '0';
      if(codeBit[2])
        flag3 = '1';
      else
        flag3 = '0';
      if(codeBit[5])
        flag4 = '1';
      else
        flag4 = '0';
      if(codeBit[6])
        flag7 = '1';
      else
        flag7 = '0';
      if(codeBit[7])
        flag8 = '1';
      else
        flag8 = '0';
      flag5 = '0';
      flag6 = '0';
    }

    roster->Setflag1(flag1);
    roster->Setflag2(flag2);
    roster->Setflag3(flag3);
    roster->Setflag4(flag4);
    roster->Setflag5(flag5);
    roster->Setflag6(flag6);
    roster->Setflag7(flag7);
    roster->Setflag8(flag8);



  • See, that's a classic WTF.  I applaud you for bringing such a disaster to us, and pity you if you have to work with such code.



  • XString er = "fout";
    Seems like a Dutch or Belgian error.



  • @snip said:

    catch(...)
      {
        XString er = "fout";
        throw er;
      }
    }
    catch(XString)
    {
      code = codeStr(0);
    }

    Wow.

    Don't tell me they do this because XString's constructor has side-effects..

    I prefer to believe it's stupidity rather than smartass-ness.

     

    Edit: or does codeStr.data() potentially throw an XString exception? Shudder...

     



  • The constructor doesn't have any side-effects and they catch XString because they throw XString. Ugh, my eyes.

    @snip said:

      catch(...)
      {
        XString er = "fout";
        throw er; // throw!
      }
    }
    catch(XString)
    {
      code = codeStr(0); // yay! catch
    }

    On a sidenote: this snippet is part of 40722 lines of code (yes, one very big cpp file).



  • Obviously never seen bitwise operators before... must be a self-taught programmer whose "Learn C++ in 21 days" book stopped just short.



  • @Aaron said:

    Obviously never seen bitwise operators before
    "code & mask" are found in the OPs code. The original author did indeed know about bit-wise and.



  • @Lingerance said:

    "code & mask" are found in the OPs code. The original author did indeed know about bit-wise and.
     

    I don't even want to think about the implications of that.  Obviously another paid-by-the-line jockey. 



  • What's sadder, the fact that this code's author use v = v * 2 to shift a bit, or that v is reconstructed from 1 for each bit?



  •  they are both products of the same ignorance of the << and >> operators.  However, I would say that they are each time created from 1 shows a deep lack of curiousity while the * 2 trick is simply the most obvious way to do it if you don't know the bitshift operators.   Then again, I guess that also proves he doesn't know the ** operator.



  • @tster said:

    Then again, I guess that also proves he doesn't know the ** operator.

    ... of C++?



  • @Spectre said:

    @tster said:
    Then again, I guess that also proves he doesn't know the ** operator.

    ... of C++?

     

    oops.  blush 



  • @tster said:

    they are both products of the same ignorance of the << and >> operators.
    He might have thought that might not be endian (sp?) portable, which is pointless for a bit flag.



  • Reminds me of some code I once wrote. But *I* was writing on a TI-82 calculator, which has no bitshift operations, and I also had to unroll loops due to speed issues. This guy however has no excuse of course... 



  • little did you know this code was going to be run on an Ruputer.



  • It's part of 40.000 lines of spaghetticode. This snippet starts at around line 12.000.

    I won't be staying around for long



  • @Lingerance said:

    @tster said:
    they are both products of the same ignorance of the << and >> operators.
    He might have thought that might not be endian (sp?) portable, which is pointless for a bit flag.

    Shift operators don't have anything to do with endianness. It's like saying 256 on some architectures is actually 652.



  • @Spectre said:

    @tster said:
    Then again, I guess that also proves he doesn't know the ** operator.

    ... of C++?

     

    Yes, this one:

    [code]int two = 2;
    int* three = new int(4);
    printf("two ** three = %d\n", two ** three);[/code]

    This prints 8, just as expected. 



  • Touché


Log in to reply