The Bit Setter

  • ParkinT 2012-05-07 08:11
    $newComment = $comment && ( ~ $first);
  • pjt33 2012-05-07 09:03
    In what way "doesn't [it] quite work as advertised"?
  • veggen 2012-05-07 09:07
    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 2012-05-07 09:10
    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 2012-05-07 09:19
    "bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal
  • Rootbeer 2012-05-07 09:22

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

    A byte with bit 4 set should be %00010000.
  • Bert 2012-05-07 09:24
    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 2012-05-07 09:26
    // set some strings that humans would consider false
    // but would be converted to true if converted by PHP
    Ah, there's TRWTF.
  • RedFox 2012-05-07 09:27
    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 2012-05-07 09:32
    //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 2012-05-07 09:33
    TRWTF is PHP...
  • Arild Kvalbein 2012-05-07 09:35
    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 2012-05-07 09:45
    My bits are not getting turned on by this code.
  • flob9 2012-05-07 09:48
    @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)
  • redtetrahedron 2012-05-07 09:59
    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.
  • dargor17 2012-05-07 10:04
    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 2012-05-07 10:04
    Maybe a third one will do the trick :)
  • simonbuchan 2012-05-07 10:06
    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 2012-05-07 10:07
    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.
  • redtetrahedron 2012-05-07 10:07
    12 = 1100
    05 = 0101
    13 = 1101
  • vt_mruhlin 2012-05-07 10:10
    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 2012-05-07 10:12
    redtetrahedron:
    12 = 1100
    05 = 0101
    13 = 1101


    I need more coffee before I just start agreeing with people.
  • Pablo 2012-05-07 10:16
    Not sure how you're converting decimal to binary or vice versa:

    12 = b1100 (1*8 + 1*4 + 0* 2 + 0*1)
    5 = b101 (you had this right: 1*4 + 0*2 + 1*1)

    12 | 5 = b1100 | b0101 = b1101 = (1*8 + 1*4 + 0*2 + 1*1) = 13

    also, 17 would be 10001 (1 * 16 + 0*8 + 0*4 + 0*2 + 1 * 1)

  • Sarten X 2012-05-07 10:21

    // 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".
  • Arancaytar 2012-05-07 10:21

    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 2012-05-07 10:27
    This was my impression. The code looks like it would work fine to me.
  • redtetrahedron 2012-05-07 10:28
    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.
  • dargor17 2012-05-07 10:40
    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 2012-05-07 10:56
    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 2012-05-07 11:02
    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 2012-05-07 11:07
    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 2012-05-07 11:12
    As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.
  • lanzz 2012-05-07 11:16
    here's a very php bitwise operation example:

    error_reporting((E_ALL | E_STRICT) & ~E_DEPRECATED)
  • null 2012-05-07 11:22
    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 2012-05-07 11:31
    Check your numbers. 12 is 1100 and 13 is 1101. 17 is 10001. You got 5 right, though.
  • sully 2012-05-07 11:34
    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 2012-05-07 11:39
    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 2012-05-07 11:48
    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.
  • Maurits 2012-05-07 12:03
    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 2012-05-07 12:06
    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 2012-05-07 12:11
    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 2012-05-07 12:20
    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 2012-05-07 12:28
    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.
  • xtremezone 2012-05-07 12:31
    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 2012-05-07 12:32
    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 2012-05-07 12:34
    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 2012-05-07 12:51
    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.
  • redtetrahedron 2012-05-07 12:52
    ¯\(°_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 2012-05-07 12:53
    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 2012-05-07 12:53
    Got it! It's BCD! :))

    0000.0101 = 5
    0001.0010 = 12
    0001.0111 = 17

    you're more of an electronics guy, aren't you?
  • Overthinker 2012-05-07 12:54
    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 2012-05-07 12:54
    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 2012-05-07 12:56
    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)
  • xtremezone 2012-05-07 13:20
    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 2012-05-07 13:22
    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.
  • galgorah 2012-05-07 13:35
    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...
  • Speakerphone Dude 2012-05-07 14:11
    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"?
  • Nagesh 2012-05-07 14:15
    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.
  • Nagesh 2012-05-07 14:18
    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 2012-05-07 14:34

    "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 2012-05-07 14:36
    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 2012-05-07 14:56
    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 2012-05-07 15:01
    lol, true that... :)
  • Paul 2012-05-07 15:04
    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 2012-05-07 15:23
    "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 2012-05-07 16:16
    Oh. My AND was wrong. Sorry about that. :|

    I am TRWTF. :(

    Captcha: Ullamcorper

    Don't even ask me what that's supposed to be...
  • Strolskon 2012-05-07 16:18
    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 2012-05-07 16:25
    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 2012-05-07 17:28
    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 2012-05-07 17:41
    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 2012-05-07 17:53
    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 2012-05-07 18:20
    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 2012-05-07 18:21
    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 2012-05-07 18:22
    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.
  • Coyne 2012-05-07 19:02
    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 2012-05-07 19:26
    The real WTF is the amount of incorrect examples of binary operators in the comments.
  • AN AWESOME CODER 2012-05-07 19:34
    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 2012-05-07 19:40
    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 2012-05-07 20:06
    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 2012-05-07 20:08
    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?!
  • lolwtf 2012-05-07 22:30
    Lesee... comment is wrong... string conversion doesn't account for "nope", "nuh-uh", "no way josé", "hell no", "FileNotFound"...
  • hikari 2012-05-08 01:09
    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 2012-05-08 01:28
    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 2012-05-08 01:37
    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.
  • Raven 2012-05-08 02:07
    "set some strings that humans would consider false...."

    Sounds like it has been programmed by a robot....
  • sa 2012-05-08 02:56
    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 2012-05-08 03:04
    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 2012-05-08 03:06
    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 2012-05-08 03:27
    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 2012-05-08 03:54
    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 2012-05-08 04:19
    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. 2012-05-08 05:42
    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. 2012-05-08 05:45
    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. 2012-05-08 05:52
    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 2012-05-08 06:01
    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.
  • dkf 2012-05-08 06:01
    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 2012-05-08 08:04
    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. 2012-05-08 08:36
    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.
  • QJo 2012-05-08 09:22
    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 2012-05-08 09:42
    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.
  • werner 2012-05-08 09:47
    L.:
    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.


    Simple. I don't. That's what URLs are for.

    Don't be a haero. Use URLs.
  • L. 2012-05-08 10:07
    werner:
    L.:
    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.


    Simple. I don't. That's what URLs are for.

    Don't be a haero. Use URLs.


    lol. Some applications need to work on IPs you know, as data and all ;)
  • hops 2012-05-08 12:52
    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...


    These values would apply to <a href="http://en.wikipedia.org/wiki/Binary-coded_decimal">BCD</a> encoding. Maurits also pointed out that 10010 is (hex) 0x12. However, the author was using the standard binary values that redtetrahedron provided.



  • Shinobu 2012-05-08 13:17
    Yeah, the string parameter is bad, and the documentation could be improved ever so slightly, but on the whole this code is okay. The WTF is not in the code - it's the existence of the function itself that is the WTF. Apparently we're dealing with a programmer whose brain shorts when he sees things like ‘$val |= $mask’. Apparently he didn't just not know what this meant, but even after figuring it out (presumably a process laborious enough to warrant the function under discussion) he still cannot remember it. I weep for him and his colleagues.
  • Evan 2012-05-08 13:25
    Shinobu:
    Yeah, the string parameter is bad, and the documentation could be improved ever so slightly, but on the whole this code is okay. The WTF is not in the code - it's the existence of the function itself that is the WTF. Apparently we're dealing with a programmer whose brain shorts when he sees things like ‘$val |= $mask’. Apparently he didn't just not know what this meant, but even after figuring it out (presumably a process laborious enough to warrant the function under discussion) he still cannot remember it. I weep for him and his colleagues.

    To be fair, it can sometimes be a good idea to create a function that does something "dumb" like this so you can pass it (as a higher-order function) to something else. Most languages don't let you pass an operator. (In fact, the only one I can think of that pretty much does this is Haskell.)

    However, based on the signature of the function in this example, I'm betting that's not the answer here.
  • Karl 2012-05-08 14:00
    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.


    You've gotta be kidding.
  • Jay 2012-05-08 14:24
    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.


    That's not a bug in the function. That's a problem with the implementation of automatic type conversions in PHP. That's like saying that if I write a function that says "return a+b" to add two numbers together, my code has a bug because if the user passes in "foobar" for "a" it will not give meaningful results. Or are you insisting that every PHP function must completely re-implement all the type conversions?
  • Jay 2012-05-08 14:37
    L.:
    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.


    It's fair to ask why the programmer found a need to do bit-twiddling. But it's not fair to ridicule him for doing bit-twiddling before you've heard the answer. Maybe he has a very sensible reason. Hey, maybe he's implementing a calculator that has AND and OR functions and it's an explicit requirement.
  • DarrenC 2012-05-08 17:02
    A couple of comments:

    1. For the record, bit numbers are usually counted from 0 (ie: The bit with the value '8' is bit 3, not 4). This way, the value of a bit is given by 2^bitnum.

    2. Why wasn't I invited to the meeting where it was decided that 'down' is a synonym for 'false'?
  • Puzzles 2012-05-08 19:09
    Meep:
    setBit($val, $bit, '0') will switch bits off, but setBit($val, $bit, 0) will switch them on. Nasty bug.

    No, it won't. The switch statement is wrapped in a if(is_string) conditional, which means that if you pass a non-string it will use PHP's built-in implicit type conversion, which will interpret the number 0 as false (according to http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting ).

    This code is a bit strange, but I don't see anything particularly WTF-worthy in it.
  • modo 2012-05-08 20:09
    dkf:
    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??

    I can take a guess at this one. It's probably fine in SQL but fails when the SQL "int" (32 bits) causes an overflow when try to stick it into a VB6 "integer" (16 bits). Been there, fixed that, rushed the fix into Prod.
  • bridget99 2012-05-08 20:17
    flob9:
    "bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal


    If I set only bit 4 of a byte, I get 00010000, or 16 decimal. The hardware devices I use call the rightmost bit "bit 0." It looks like whoever wrote that PHP started from "bit 1," which strikes me as odd, but is probably not unheard of.

    These bit numbers most definitely are in decimal, though. I've never seen anyone call the leftmost bit of a 16-bit number "bit F," for example... it's bit 15. (I suppose this is one of the many things about which I can say, "no one would ever X, except maybe a poster from The Daily WTF.")

    Incidentally, I don't consider the code originally posted that much of a WTF, unless it has some kind of bug that I don't see. (I know nothing of PHP).

    The function makes heroic efforts to deal with inconsistent input formats. That doesn't seem like the sort of thing that ought to require an apology; the author of this code was just engaging in defensive programming.

  • Shinobu 2012-05-09 00:33
    Evan:
    ... create a function that does something "dumb" like this so you can pass it (as a higher-order function) to something else.
    However, based on the signature of the function in this example, I'm betting that's not the answer here.
    I entertained the thought for a moment. Just a moment.
    I also thought maybe it could have been for something like (pseudocode alert):
    on(checkbox.click)
    
    {
    setbit(settings, 200h, checkbox);
    }
    Which could be a slight improvement over this:
    on(checkbox.click)
    
    {
    if(checkbox) settings |= 200h;
    else settings &= ~200h;
    }
    But in the end I didn't consider that a likely scenario. If this is what happened though, I take everything back and declare Nathan TRWTF.
  • L. 2012-05-09 01:21
    Evan:
    Shinobu:
    Yeah, the string parameter is bad, and the documentation could be improved ever so slightly, but on the whole this code is okay. The WTF is not in the code - it's the existence of the function itself that is the WTF. Apparently we're dealing with a programmer whose brain shorts when he sees things like ‘$val |= $mask’. Apparently he didn't just not know what this meant, but even after figuring it out (presumably a process laborious enough to warrant the function under discussion) he still cannot remember it. I weep for him and his colleagues.

    To be fair, it can sometimes be a good idea to create a function that does something "dumb" like this so you can pass it (as a higher-order function) to something else. Most languages don't let you pass an operator. (In fact, the only one I can think of that pretty much does this is Haskell.)

    However, based on the signature of the function in this example, I'm betting that's not the answer here.


    As Haskell is all moderny and stuff and according to your comment, wouldn't that be because operators are wrapped in functions in Haskell - or something equivalent ?
  • Apologist 2012-05-09 09:29
    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...


    Some people I won't name (yet!) used an int field of booleans for security access rights - read, write, admin. Spot the extra deliberate mistake? :-)
  • Jay 2012-05-09 16:34
    Apologist:
    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...


    Some people I won't name (yet!) used an int field of booleans for security access rights - read, write, admin. Spot the extra deliberate mistake? :-)


    I think those people are commonly referred to as, "the inventors of Unix". I think Unix file permissions are routinely packed as bit-flags in an int.
  • Evan 2012-05-09 17:10
    L.:
    As Haskell is all moderny and stuff and according to your comment, wouldn't that be because operators are wrapped in functions in Haskell - or something equivalent ?

    I'm not quite sure what your question means, really. Conceptually the difference between an operator and function is surface-level syntax.

    I'll say a couple things about what Haskell does, and you can decide if it's helpful for your understanding or not. (Disclaimer: the code below isn't compiled, so it may not be legal. But the idea is there.)

    Suppose I have a function compute that takes a binary function, applies it to 1 and 2, then returns the result:
    compute :: (Integer -> Integer) -> Integer
    
    compute f a b = (f 1 2)


    What you can't do is pass "+" to it, because "+" is an operator, not a function:
    compute +  -- error

    (-- marks a comment)

    Now, I can write a function "add" that does what it says, then use that to call "compute":
    add :: Integer -> Integer -> Integer
    
    add a b = a + b
    compute add -- evaluates to 3


    but I can also get a function from an operator really easily, by using "(+)":
    compute (+)  -- evaluates to 3


    (Incidentally, you can also fix one operand. "(+)" gives a function that takes two ints and gives their sum; "(+8)" gives a function that takes one int and returns that plus 8. Haskell calls this a "section.")

    You can also go the other way; by putting backticks around a binary function, you can use it as a binary operator:
    1 `add` 2  -- evaluates to 3
  • Shinobu 2012-05-12 00:24
    Here's to hoping that I'll never have to use Haskell. Reminds me too much of Perl - a language seemingly designed by an Egyptologist.
  • PunctuallyChallenged 2012-05-13 07:47
    We're looking forward to reading your code in an upcoming TDWTF!
  • Jis 2012-05-16 11:31
    I like how he clearly used his own function to run the values he mentions in the comments, instead of documenting how it should really work!
  • History Teacher 2012-05-20 10:00
    werner:

    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.

    Oh come on. It's quite possible to need to for example create physical ID label strings with PHP, where the ID needs to code some info, yet be short. Or handle some raw binary data, such as protocol headers. Or decode esoteric data such as comma separated numbers into raw binary. Or any number of things where PHP is just perfect tool, if you need other people to understand the code later (which rules out Perl ;-).
  • ___ 2012-05-25 22:19
    Sorry, but no.
    neither gmp_setbit in PHP nor the underlying mpz_setbit function in the GMP library work with a bit mask.
    Actually the example given in the reference you provided contradicts your statement.
    For the really interested take a look at PHP source code line 1453 (implementation of gmp_setbit)
    and mpz_setbit from the GMP lib

    In my opinion this is also the WTF...