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

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

    There. Fixed. :)

    Captcha: secundum - frist, secundum, ...

  • gnomie (unregistered) in reply to Maurits
    Maurits:
    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)

    No... not really... never was...

  • Fonseca (unregistered) in reply to Fonseca
    Fonseca:
    Got it! It's BCD! :))

    0000.0101 = 5 0001.0010 = 12 0001.0111 = 17

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

    (I'm referring to Dargor17's post, of course)

  • (cs)

    TRWTF is that months ago I wrote a C# class to manipulate a bitmask's state as booleans indexed by Enum. :-o

    enum Foo { None = 0x0, Bar = 0x1, Baz = 0x2, BarBaz = 0x3, Bak = 0x4 }
    Bitmask b = new Bitmask(Foo.Bar);
    if(b[Foo.Bar])
    {
        // ...
    }
    b[Foo.BarBaz] = true;
    if(b[Foo.Baz])
    {
        // ...
    }
    I don't remember which project it was for, or if any version ever went into production though... I just now went searching for the code, barely found it hidden away in an archive, and fixed a silly bug in it that I think had me stumped when I first wrote it. xD grep doesn't find it anywhere in my ~/src tree so I guess that's a good sign.

    Though the code could arguably be useful (particularly for colleagues that have trouble with bitwise logic), I'm not sure that the overhead of a heap-allocated object to manage a bitmask doesn't outweigh the advantages of using a bitmask... I don't remember why I originally wrote this either, but I suspect that it's because C# doesn't allow bitwise operations on enum types directly, and all of the conversions to an integer type and back to an Enum made the code ugly and error prone...

  • Jack (unregistered)

    This function does work exactly as advertised. Maybe if the author had called the second argument $mask instead of $bit, you would not be so confused.

    I'm not saying it's not a silly function, I'm just saying there are no bugs.

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

    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...

  • (cs) in reply to Arancaytar
    Arancaytar:
    Also, he left out "bad", "zero", "negative" and "do'neh'lini".

    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?

  • (cs) 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... let alone custom made functions for that.

    bit-wise operation is mostly requested in all program languages. once requested, you must code for it.

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

    with more business like leaning in IT department, less it and more process take place. so bit manipulate is only something that was in 1st year of comp sci. now we pass taht paper, who care about little bits?

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

    Um... because 12 is 1100 and 5 is 101, so the result is 1101?

    10010 is 16+2, or 18 (decimal). Now, given that you did the final OR computation and got 10111 (wrong, but work with it), 10111 is 16+4+2+1 or 23 (not 17). But we did need a Real WTF out of this story (the function is horribly misguided, but not really WTF worthy).

  • flob9 (unregistered) in reply to galgorah
    galgorah:
    Black Bart:
    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.

    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...

    Tried the "SET" datatype someday ... I cried. How do you handle 20 boolean flags in a table ? (I was using libmysql & c++)

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

    As far as you know? I can't really say I find that reassuring.

    What I do find reassuring is a compiler or interpreter which sees my attempt to treat a string value as boolean true/false for the glaring cock-up that it is, and isn't afraid to tell me so.

  • Jodo (unregistered) in reply to tom103

    lol, true that... :)

  • Paul (unregistered) in reply to Arancaytar
    Arancaytar:
    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".

    I'm trying to work out what is so utterly heinous about something like "function setBit(int $val, int $bit, bool $switch = true)" that it's worth all that kitten-slaying boilerplate code to catch the possibility that someone will pass a string where a boolean is called for?

    Also, what about 'False', 'FALSE', 'fAlSe'...?

  • fomori (unregistered)

    "smarter-than-built-in-language-features collegue" ...

    i am quite sure that php built-ins looked that way, actually. looks like a promising php6 feature. but by then they probably improved the function.

    real_setBit($needle, $haystack) ...

  • AGray (unregistered) in reply to Overthinker

    Oh. My AND was wrong. Sorry about that. :|

    I am TRWTF. :(

    Captcha: Ullamcorper

    Don't even ask me what that's supposed to be...

  • (cs) in reply to Speakerphone Dude
    Speakerphone Dude:
    Arancaytar:
    Also, he left out "bad", "zero", "negative" and "do'neh'lini".

    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?

    Take the code from a chatbot (cleverbot, ELIZA, whatever) and use it to parse all arguments to functions from now on.

    I'm sure you could create an interesting programming language based on that.

  • Evan (unregistered) in reply to Speakerphone Dude
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.
  • Your Name (unregistered) in reply to galgorah
    galgorah:
    Black Bart:
    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.

    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...
    Yep, I used to work on a system which actually does that with security permissions from checkboxes, just like you said. Eventually some of the numbers got into the millions. Amazing how when they spent several years rewriting it from scratch, that's one area they swore they'd do away with, but nope it's still there.

    Captcha "vereor" - when regular "or" isn't powerful enough for your bit manipulation

  • Rabble (unregistered) in reply to Maurits

    0x12 is hex, not dec 12. The example refers to decimal 12.

    (HEX) 0x12 -> 0b00010010 (DEC) 12 -> 0b00001100

    The bitmath does, indeed, work out the examples given.

    TRWTF is the number of folks here that don't understand bit twiddling..

  • Jack (unregistered) in reply to Evan
    Evan:
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.

    "It is known."

  • Davey Jones (unregistered) in reply to lanzz

    I actually consider that a very non-php idiom. It seems very C/C++ish to me. Php seems like it attempts to be a 'higher level' language, and shouldn't need to be as concerned with bitwise manipulation.

  • null (unregistered) in reply to Jack
    Jack:
    Evan:
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.

    "It is known."

    "Make it so."

  • TheReal(TM) Anonymous (unregistered)

    The code isn't a WTF. It works as advertised by the clumsily written comment.

    If there's a WTF, then it's the apparent lack of familiarity with basic bit arithmetic on the coder's side, though he did manage to produce correctly working code in the end.

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

    Well, t's over-engineered, the description is confusing, and so is the name. I guess that rates a WTF.

    But for the rest, you're right: Functionally, it does exactly what it promises to do, if one carefully reads the description--and doesn't jump to conclusions about what "turn on the 4 bit" means. After all, 8 | 4 is 12.

  • ydrol (unregistered)

    The real WTF is the amount of incorrect examples of binary operators in the comments.

  • AN AWESOME CODER (unregistered) in reply to vt_mruhlin
    vt_mruhlin:
    would you rather have buggy code or buggy documentation?

    Depends.

    In this case, it's one in the same. If this is some library code that I need to use for some presently-unknown reason and I expect it to work as documented, the code is buggy.

  • AN AWESOME CODER (unregistered) in reply to Davey Jones
    Davey Jones:
    I actually consider that a very non-php idiom. It seems very C/C++ish to me. Php seems like it attempts to be a 'higher level' language, and shouldn't need to be as concerned with bitwise manipulation.

    This is the dumbest assumption of purpose I've ever heard in my life.

    We're not talking about memory management, we're talking about math.

  • Meep (unregistered) in reply to pjt33
    pjt33:
    In what way "doesn't [it] quite work as advertised"?

    setBit($val, $bit, '0') will switch bits off, but setBit($val, $bit, 0) will switch them on. Nasty bug.

  • Meep (unregistered) in reply to Meep
    Meep:
    pjt33:
    In what way "doesn't [it] quite work as advertised"?

    setBit($val, $bit, '0') will switch bits off, but setBit($val, $bit, 0) will switch them on. Nasty bug.

    No, I take that back.

    What the hell kind of WTF has working code?!

  • (cs)

    Lesee... comment is wrong... string conversion doesn't account for "nope", "nuh-uh", "no way josé", "hell no", "FileNotFound"...

  • (cs) in reply to flob9
    flob9:
    //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];

    In my previous job I had to maintain code written by someone who did things like that. He also deliberately threw exceptions to break out of loops.

  • Gibbon1 (unregistered) in reply to vt_mruhlin
    vt_mruhlin:
    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.

    I have a macro in C, BF(X,Y), returns an int with bits X through Y set. Course that's C. So a two letter macro is culturally appropriate. This is PHP, so a function that does something only somewhat related to what one would assume based on the name is also culturally appropriate. First we have setBit() which sets a bit field. Eventually we'll have Setsbit() which sets one bit, starting with bit 'zero'

    I think the WTF here is assuming anything about anything in PHP. PHP is a language for a certain type of customer, one that is allergic to committing to spending enough money up front, yet can be persuaded to spend double that in drips and drabs over time.

  • Dick Head (unregistered)

    I think it would be less WTF to split the functionality into two functions - setBit and clearBit to do away with the boolean parameter cruft.

  • (cs)

    "set some strings that humans would consider false...."

    Sounds like it has been programmed by a robot....

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

    uhm.... 12 (1100) & 5 (0101) = 13 (1101)

    oh, I see, you're a troll!

  • Captcha:genitus (unregistered) in reply to Meep
    Meep:
    pjt33:
    In what way "doesn't [it] quite work as advertised"?

    setBit($val, $bit, '0') will switch bits off, but setBit($val, $bit, 0) will switch them on. Nasty bug.

    TRWTF is all these funky conversions. This function accepts a boolean, accept PHP's type casting or give it a boolean

  • dxf (unregistered) in reply to Nagesh
    Nagesh:
    gnomie:
    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).

    with more business like leaning in IT department, less it and more process take place. so bit manipulate is only something that was in 1st year of comp sci. now we pass taht paper, who care about little bits?

    I'm not sure if I understood correctly, but I agree with how I undertand it.

    Too much learning in higher level language will always ee people unknowing of the truth fundamental.

  • werner (unregistered) in reply to Evan
    Evan:
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.

    Seconded. I just added

    #define AS_FORETOLD_BY_THE_PROPHECY true
    

    to my WTFutils.h

    What bothers me about this post is that bit twiddling is supposed to be a fairly low level construct, mostly used for premature optimization in C/C++ code, while Php is a high level language. The fact that someone needed such a function in Php in the first place is TRWTF.

  • peter (unregistered) in reply to sa
    sa:
    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...

    uhm.... 12 (1100) & 5 (0101) = 13 (1101)

    oh, I see, you're a troll!

    and a troll with 16 fingers too.

  • toon (unregistered) in reply to RedFox
    RedFox:
    I'd understand if this was C and there were macros like set_bit, unset_bit, toggle_bit.

    Now there's a WTF if I've ever seen one.

  • L. (unregistered) in reply to galgorah
    galgorah:
    Black Bart:
    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.

    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...

    Bitmasks have been used to store a bunch of booleans in the past, it's not all that bad, even if your example shows some people can do it the fail way.

  • L. (unregistered) in reply to flob9
    flob9:
    galgorah:
    Black Bart:
    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.

    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...

    Tried the "SET" datatype someday ... I cried. How do you handle 20 boolean flags in a table ? (I was using libmysql & c++)

    MySQL is TRWTF

  • L. (unregistered) in reply to werner
    werner:
    Evan:
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.

    Seconded. I just added

    #define AS_FORETOLD_BY_THE_PROPHECY true
    

    to my WTFutils.h

    What bothers me about this post is that bit twiddling is supposed to be a fairly low level construct, mostly used for premature optimization in C/C++ code, while Php is a high level language. The fact that someone needed such a function in Php in the first place is TRWTF.

    WTF. and how, dear sir, do you plan on handling ip addresses without binary ???

    CAPTCHA conventio : welcome to conventio, the convention hall planet.

  • bitbitbit (unregistered) in reply to peter
    peter:
    sa:
    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...

    uhm.... 12 (1100) & 5 (0101) = 13 (1101)

    oh, I see, you're a troll!

    and a troll with 16 fingers too.

    More likely 10 fingers and only 6 toes.

  • (cs) in reply to galgorah
    galgorah:
    I've seen exactly this. Its terrible. Checkboxes on a web form that get "converted" to individual bits packed into an integer and sent to the database. As soon as you added a new checkbox into the form you had problems in the backend...
    Your database has problems with integers that don't fit in 32 bits? Your forms have more than 32 checkboxes??
  • Carl (unregistered) in reply to Your Name
    Your Name:
    Yep, I used to work on a system which actually does that with security permissions from checkboxes, just like you said. Eventually some of the numbers got into the millions. Amazing how when they spent several years rewriting it from scratch, that's one area they swore they'd do away with, but nope it's still there.
    But it would be so hard to store each checkbox separately!

    I actually got a similar whine from my devs recently: But that's another 20 database columns!

    I wanted to ask: So the vendor now charges another $10,000 license for each new column? Or what? Surely you don't have to write a gob of new code for each column, do you? Ever hear of reusable stuff? One validation module for ALL dates, maybe?

  • L. (unregistered) in reply to Carl
    Carl:
    Your Name:
    Yep, I used to work on a system which actually does that with security permissions from checkboxes, just like you said. Eventually some of the numbers got into the millions. Amazing how when they spent several years rewriting it from scratch, that's one area they swore they'd do away with, but nope it's still there.
    But it would be so hard to store each checkbox separately!

    I actually got a similar whine from my devs recently: But that's another 20 database columns!

    I wanted to ask: So the vendor now charges another $10,000 license for each new column? Or what? Surely you don't have to write a gob of new code for each column, do you? Ever hear of reusable stuff? One validation module for ALL dates, maybe?

    The real wtf is you want to store a bunch of boolean values. At least the bitmask is a sensible technical solution that partially hides the spupidity that 20 boolean columns would instantly give away.

    While I wouldn't go as far as to defend your code monkeys, I'm pretty sure you're full of wtf and don't even realize how incredibly nice a bitmask can be.

  • (cs) in reply to werner
    werner:
    Evan:
    Speakerphone Dude:
    A TRUE catch-all is great because it will catch "possibly", "you betcha" and "as foretold by the prophecy"; however what about "unlikely" or "you wish"? Also what about the situations where the context (or non-verbal) is needed to make a good call on the meaning, like "maybe" or "sure"?
    I think I have to use "as foretold by the prophecy" instead of True in Python now.

    Seconded. I just added

    #define AS_FORETOLD_BY_THE_PROPHECY true
    

    to my WTFutils.h

    What bothers me about this post is that bit twiddling is supposed to be a fairly low level construct, mostly used for premature optimization in C/C++ code, while Php is a high level language. The fact that someone needed such a function in Php in the first place is TRWTF.

    What about "Amen", "Insh'allah", "So mote it be" and "Das ist gut, ja?"?

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to bitbitbit
    bitbitbit:
    peter:
    and a troll with 16 fingers too.
    More likely 10 fingers and only 6 toes.
    Just a very inept yakuza. They had to cut off both his pinky fingers AND his pinky toes.

Leave a comment on “The Bit Setter”

Log In or post as a guest

Replying to comment #:

« Return to Article