- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
$newComment = $comment && ( ~ $first);
Admin
In what way "doesn't [it] quite work as advertised"?
Admin
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.
Admin
Mostly to deal with some numb-nut creating a field of booleans packed into an INT in the database.
Admin
"bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal
Admin
Isn't it conventional for the least significant bit to be enumerated as 0?
A byte with bit 4 set should be %00010000.
Admin
There's no bit shifting involved. So if you wanted to set the 7th bit, you'd call
and it would return , which is not setting the 7th bit (it is setting the first 3 bits).Admin
Admin
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:
Admin
//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];
Admin
TRWTF is PHP...
Admin
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)
Admin
My bits are not getting turned on by this code.
Admin
@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)
Admin
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.
Admin
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...
Admin
Maybe a third one will do the trick :)
Admin
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?
Admin
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.
Admin
12 = 1100 05 = 0101 13 = 1101
Admin
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?
Admin
I need more coffee before I just start agreeing with people.
Admin
Not sure how you're converting decimal to binary or vice versa:
12 = b1100 (18 + 14 + 0* 2 + 01) 5 = b101 (you had this right: 14 + 02 + 11)
12 | 5 = b1100 | b0101 = b1101 = (18 + 14 + 02 + 11) = 13
also, 17 would be 10001 (1 * 16 + 08 + 04 + 0*2 + 1 * 1)
Admin
Obviously, this would only be considered working if it accounted for "FileNotFound".
Admin
...
A little known fact is that every time someone does this, an innocent little kitten dies. :(
Addendum (2012-05-07 10:27):
...
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".
Admin
This was my impression. The code looks like it would work fine to me.
Admin
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.
Admin
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.
Admin
Admin
Truth table:
OR and XOR, not OR and AND. ...
captcha: facilisis
Admin
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.
Admin
As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.
Admin
here's a very php bitwise operation example:
error_reporting((E_ALL | E_STRICT) & ~E_DEPRECATED)
Admin
Admin
Check your numbers. 12 is 1100 and 13 is 1101. 17 is 10001. You got 5 right, though.
Admin
"Correctly". In no sense is doing that correct!
Admin
Admin
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.
Admin
To be fair:
0x12 (= 0b10010) | 0x5 (= 0b101) is indeed 0x17 (0b10111) and not 0b13 (0b10011)
Admin
Your truth table is lying.
Admin
Admin
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!
Admin
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?
Oh, yeah, I guess it can.
Admin
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).
Admin
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.
Admin
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).
Admin
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.
Admin
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.
Admin
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).
Admin
Got it! It's BCD! :))
0000.0101 = 5 0001.0010 = 12 0001.0111 = 17
you're more of an electronics guy, aren't you?