• werner (unregistered) in reply to L.
    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. (unregistered) in reply to werner
    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 (unregistered) in reply to vt_mruhlin
    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 BCD encoding. Maurits also pointed out that 10010 is (hex) 0x12. However, the author was using the standard binary values that redtetrahedron provided.

  • Shinobu (unregistered)

    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 (unregistered) in reply to Shinobu
    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 (unregistered) 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.

    You've gotta be kidding.

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

    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 (unregistered) in reply to L.
    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 (unregistered)

    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 (unregistered) in reply to Meep
    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 (unregistered) in reply to dkf
    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.
  • (cs) in reply to flob9
    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 (unregistered) in reply to Evan
    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. (unregistered) in reply to Evan
    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 (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...

    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 (unregistered) in reply to Apologist
    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 (unregistered) in reply to L.
    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 (unregistered)

    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 (unregistered) in reply to Jerry

    We're looking forward to reading your code in an upcoming TDWTF!

  • Jis (unregistered)

    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 (unregistered) in reply to werner
    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 ;-).
  • ___ (unregistered) in reply to redtetrahedron

    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...

Leave a comment on “The Bit Setter”

Log In or post as a guest

Replying to comment #:

« Return to Article