• (cs)

    $newComment = $comment && ( ~ $first);

  • (cs)

    In what way "doesn't [it] quite work as advertised"?

  • (cs)

    I know I am TRWTF, but I can't come up with a scenario where I'd need bitwise operations in PHP... let alone custom made functions for that.

  • Black Bart (unregistered) in reply to veggen
    veggen:
    I know I am TRWTF, but I can't come up with a scenario where I'd need bitwise operations in PHP

    Mostly to deal with some numb-nut creating a field of booleans packed into an INT in the database.

  • flob9 (unregistered) in reply to pjt33

    "bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal

  • (cs) in reply to flob9

    Isn't it conventional for the least significant bit to be enumerated as 0?

    A byte with bit 4 set should be %00010000.

  • Bert (unregistered) in reply to pjt33

    There's no bit shifting involved. So if you wanted to set the 7th bit, you'd call

    setBit($val, 7)
    and it would return
    $val | 7
    , which is not setting the 7th bit (it is setting the first 3 bits).

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered)
    // set some strings that humans would consider false // but would be converted to true if converted by PHP
    Ah, there's TRWTF.
  • RedFox (unregistered)

    I really hate the unnecessary multifunctional input. Who would write a function like that? Who would ever write setBit($value, 1, 'false') ??? That's TRWTF.

    I'd understand if this was C and there were macros like set_bit, unset_bit, toggle_bit.

    The correct PHP version of this WTF would be:

    if ($switch) $newval = $val | (1<<$bit); // bit on else $newval = $val & ~(1<<$bit); // bit off
  • flob9 (unregistered)

    //fixed for you

    mysql_connect(); if($switch) $ret = mysql_fetch_array(mysql_query("SELECT $val | $bit LIMIT 1"); else $ret = mysql_fetch_array(mysql_query("SELECT $val & ~($bit) LIMIT 1"); return $ret[0];

  • tom103 (unregistered)

    TRWTF is PHP...

  • Arild Kvalbein (unregistered)

    Note that it says "all the bits specified by $bit", so $bit is not the ordinal of a single bit, but rather a bit-mask. Also, the example says "turn on the 4 bit", not "turn on the 4th bit", so it is referring to bit 2 (with the least significant bit being 0)

  • Silent D (unregistered)

    My bits are not getting turned on by this code.

  • flob9 (unregistered) in reply to Arild Kvalbein

    @Arild Kvalbein> true but confusing. I usually refer bit 1 to the first lower bit.

    BTW the WTF is the comments not clear at all :

    php -r "echo sprintf('%08b',4);" 00000100 (ok, but comment confusing; bit 4 = 4th bit for me, not bit represented by decimal value 4)

    php -r "echo sprintf('%08b',1 << 4);" 00010000 (what i had expected with the function name)

  • (cs)

    OK, it's Monday morning here, and I'm still working on my second cup of coffee, (and I am not familiar with PHP), but I don't see anything wrong with this code that would warrant it being published as a WTF.

  • (cs)

    In a sense, the function works exactly as advertised by the comment: it's a clusterfuck of broken logic and stupidity.

    I'm puzzled at how he explains that if $val is 12(10010) and $bit is 5(101) he would get 13(10011): I would have expected 17(10111), assuming that $bit is a mask as is implied by the other comment...

  • QD (unregistered) in reply to redtetrahedron

    Maybe a third one will do the trick :)

  • simonbuchan (unregistered)

    Seriously, the only problem with this code is the guys phrasing of which bit it is, and maybe that it's not "setBits". Oh, and that apparently his shop has a habit of just trying to throw random strings at Boolean arguments. Perhaps the submitter is one of those people?

  • vt_mruhlin (unregistered)

    I guess it "doesn't work as advertised" because it's named setBit instead of setBits and the $bit parameter would more appropriately be named $bitMask. Not much of a WTF though.

    Those of you who seem to be expecting it to set the nth bit should really consider the usefulness of setting multiple bits at the same time.

  • (cs) in reply to dargor17

    12 = 1100 05 = 0101 13 = 1101

  • vt_mruhlin (unregistered) in reply to dargor17
    dargor17:
    In a sense, the function works exactly as advertised by the comment: it's a clusterfuck of broken logic and stupidity.

    I'm puzzled at how he explains that if $val is 12(10010) and $bit is 5(101) he would get 13(10011): I would have expected 17(10111), assuming that $bit is a mask as is implied by the other comment...

    Hmm, yeah I somehow missed that math error. That does account for the "not working as advertised" part. Ah well, would you rather have buggy code or buggy documentation?

  • vt_mruhlin (unregistered) in reply to redtetrahedron
    redtetrahedron:
    12 = 1100 05 = 0101 13 = 1101

    I need more coffee before I just start agreeing with people.

  • Pablo (unregistered) in reply to dargor17

    Not sure how you're converting decimal to binary or vice versa:

    12 = b1100 (18 + 14 + 0* 2 + 01) 5 = b101 (you had this right: 14 + 02 + 11)

    12 | 5 = b1100 | b0101 = b1101 = (18 + 14 + 02 + 11) = 13

    also, 17 would be 10001 (1 * 16 + 08 + 04 + 0*2 + 1 * 1)

  • Sarten X (unregistered) in reply to pjt33
        // set some strings that humans would consider false
        // but would be converted to true if converted by PHP
        if (is_string($switch)) {
            switch ($switch) {
                case 'false' :
                case 'down' :
                case 'off' :
                case 'not' :
                case '0' :
                case '' :
    

    Obviously, this would only be considered working if it accounted for "FileNotFound".

  • (cs)
    if (is_string($switch)) { switch ($switch) { case 'false' : case 'down' : case 'off' : case 'not' : case '0' : case '' :

    ...

    A little known fact is that every time someone does this, an innocent little kitten dies. :(

    Addendum (2012-05-07 10:27):

    if (is_string($switch)) { switch ($switch) { case 'false' : case 'down' : case 'off' : case 'not' : case '0' : case '' :

    ...

    A little known fact is that every time someone does this, an innocent little kitten dies. :(

    Also, he left out "bad", "zero", "negative" and "do'neh'lini".

  • KJ (unregistered) in reply to Arild Kvalbein

    This was my impression. The code looks like it would work fine to me.

  • (cs) in reply to Bert
    Bert:
    There's no bit shifting involved. So if you wanted to set the 7th bit, you'd call
    setBit($val, 7)
    and it would return
    $val | 7
    , which is not setting the 7th bit (it is setting the first 3 bits).

    So the problem is the author didn't say "bit mask" in the description? He didn't say it was bit shifting either. It's obvious from the description what this does. Yawn.

  • (cs) in reply to Pablo

    Ooops... I felt too lazy to check the conversions and just used the gnome calculator without noticing I had left it with hex input (meaning that instead of 12 I was looking at 0x12 etc)... Ok, my bad. I guess it's not so WTFy, then, even though it's still a bit messy.

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to redtetrahedron
    redtetrahedron:
    OK, it's Monday morning here, and I'm still working on my second cup of coffee, (and I am not familiar with PHP), but I don't see anything wrong with this code that would warrant it being published as a WTF.
    I would start with its very existence as being the first WTF. This is functionizing a basic feature of the language, and doing it in a bad way.
  • AGray (unregistered)

    Truth table:

     A | B | A OR B | A AND (NOT B)
    --------------------------------
     0   0     0          0
     0   1     1          1
     1   0     1          1
     1   1     1          0
    

    OR and XOR, not OR and AND. ...

    captcha: facilisis

  • JAPH (unregistered)

    TRWTF is that there isn't a function to convert those strings to a boolean, right? I mean, can you imagine updating all the functions if someone were to add another false string value?

    esse: Portuguese for "this", as in FTW.

  • PHPer (unregistered)

    As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.

  • lanzz (unregistered) in reply to veggen

    here's a very php bitwise operation example:

    error_reporting((E_ALL | E_STRICT) & ~E_DEPRECATED)

  • null (unregistered) in reply to dargor17
    dargor17:
    I'm puzzled at how he explains that if $val is 12(10010) and $bit is 5(101) he would get 13(10011): I would have expected 17(10111), assuming that $bit is a mask as is implied by the other comment...
    I'm actually puzzled at how you think 12 is 10010, 13 is 10011, or that 17 is 10111? Suggestion: if you failed CS 101, don't bother posting comments.
  • sully (unregistered) in reply to dargor17

    Check your numbers. 12 is 1100 and 13 is 1101. 17 is 10001. You got 5 right, though.

  • sully (unregistered) in reply to PHPer
    As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.

    "Correctly". In no sense is doing that correct!

  • Paul Neumann (unregistered) in reply to vt_mruhlin
    vt_mruhlin:
    dargor17:
    In a sense, the function works exactly as advertised by the comment: it's a clusterfuck of broken logic and stupidity.

    I'm puzzled at how he explains that if $val is 12(10010) and $bit is 5(101) he would get 13(10011): I would have expected 17(10111), assuming that $bit is a mask as is implied by the other comment...

    Hmm, yeah I somehow missed that math error. That does account for the "not working as advertised" part. Ah well, would you rather have buggy code or buggy documentation?

    No.

  • Tyler (unregistered)

    Not sure about this one either, looks like it works and contains documentation stating what it does complete with examples for any beginners that don't understand binary. Unusually protective of mis-use with the switch statement but I'd rather have someone that over-protects rather than under-protects.

  • (cs) in reply to dargor17
    dargor17:
    In a sense, the function works exactly as advertised by the comment: it's a clusterfuck of broken logic and stupidity.

    I'm puzzled at how he explains that if $val is 12(10010) and $bit is 5(101) he would get 13(10011): I would have expected 17(10111), assuming that $bit is a mask as is implied by the other comment...

    To be fair:

    0x12 (= 0b10010) | 0x5 (= 0b101) is indeed 0x17 (0b10111) and not 0b13 (0b10011)

  • Ben Jammin (unregistered) in reply to AGray
    AGray:
    Truth table:
     A | B | A OR B | A AND (NOT B)
    --------------------------------
     0   0     0          0
     0   1     1          1
     1   0     1          1
     1   1     1          0
    

    OR and XOR, not OR and AND. ...

    captcha: facilisis

    Your truth table is lying.

  • Evan (unregistered) in reply to flob9
    flob9:
    "bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal
    Interestingly enough the code does not use the term "bit 4".
  • Jerry (unregistered)

    When programming, and especially with complicated logic like this that many people won't readily understand, it is more important to write your code so it is clear to all what is going on. Forget the fancy symbols... is it & or + or maybe && for "and", | or || or whatever for "or", ^ or ! or - for "not"... see even I can't remember. Some people work in more than one language you know!

    So anyway, he should have converted all the inputs to strings of "1"s and "0"s (fixed length) and then looped through each character position testing each possible combination:

    if this is 1 and that is 1 and the switch says blah blah...

    Much easier to maintain!

  • Fred (unregistered)
            case 'false' :
            case 'down' :
            case 'off' :
            case 'not' :
            case '0' :
            case '' :
                $switch = false;
    

    That's bad enough code smell right there. A vague API? A list that can't ever possibly be complete? Can it get any worse?

           default :
                $switch = true;
    

    Oh, yeah, I guess it can.

  • (cs)

    The comment is pretty straight forward, and describes what the function does precisely, AFAICT. It seems everyone calling this a WTF has trouble with bitwise logic. ^^ Good thing a kind soul came along and wrote you this function!

    The embedded deserialization is TRWTF, and should be abstracted out into a separate function (if used at all).

  • bkDJ (unregistered)

    The wording might be weird, since "mask" is never mentioned but this looks like code that works with complete documentation, even with a reasonable example to clear up any misunderstandings caused by, again, weird wording. But it looks like it works exactly as advertised. TRWTF is whoever is responsible for filtering shit submissions out from becoming front-page articles.

  • Coder (unregistered)

    Although the function is a software design oddity it does correctly apply a bit mask to a value. The post implies that the author of the code does not understand bit-wise operators but that is not the case. As mentioned before, $bit is not being used as an ordinal for a single bit but as a bit mask. The documentation does describe this (although not as clearly as it should).

  • Jay (unregistered)

    To the people saying that they don't see the WTF here:

    A programmer put a comment block at the top of a function that completely and accurately describes what the function does, including examples in case anyone was confused by the wording (like "bit number versus bit mask").

    This is followed by code that is well-structured, clear, and easy to read.

    Is this guy insane?! If we let one programmer get away with that, soon they'll be expecting all of us to do it! Next thing you know, some crazy person will be demanding that we update comments when we change the code! Who knows what could come after that. Version control? Testing? Showing up for work on time? Nazi concentration camps?

    This person must be stopped now, before it's too late.

  • (cs) in reply to ¯\(°_o)/¯ I DUNNO LOL
    ¯\(°_o)/¯ I DUNNO LOL:
    redtetrahedron:
    OK, it's Monday morning here, and I'm still working on my second cup of coffee, (and I am not familiar with PHP), but I don't see anything wrong with this code that would warrant it being published as a WTF.
    I would start with its very existence as being the first WTF. This is functionizing a basic feature of the language, and doing it in a bad way.

    If you're referring to gmp-setbit (thx Google), this version works by bit mask instead of an ordinal of a bit, so more than one bit can be set/cleared at once.

  • gnomie (unregistered) in reply to Maurits

    To be fair: A lot commenters seem to be less familiar with bit manipulation than the author of the supposedly WTF code snippet (which is actually hardly WFT at all).

  • Fonseca (unregistered) in reply to null

    Got it! It's BCD! :))

    0000.0101 = 5 0001.0010 = 12 0001.0111 = 17

    you're more of an electronics guy, aren't you?

Leave a comment on “The Bit Setter”

Log In or post as a guest

Replying to comment #380463:

« Return to Article