- 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
There. Fixed. :)
Captcha: secundum - frist, secundum, ...
Admin
No... not really... never was...
Admin
(I'm referring to Dargor17's post, of course)
Admin
TRWTF is that months ago I wrote a C# class to manipulate a bitmask's state as booleans indexed by Enum. :-o
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...
Admin
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.
Admin
Admin
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"?
Admin
bit-wise operation is mostly requested in all program languages. once requested, you must code for it.
Admin
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?
Admin
"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).
Admin
Tried the "SET" datatype someday ... I cried. How do you handle 20 boolean flags in a table ? (I was using libmysql & c++)
Admin
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.
Admin
lol, true that... :)
Admin
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'...?
Admin
"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) ...
Admin
Oh. My AND was wrong. Sorry about that. :|
I am TRWTF. :(
Captcha: Ullamcorper
Don't even ask me what that's supposed to be...
Admin
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.
Admin
Admin
Captcha "vereor" - when regular "or" isn't powerful enough for your bit manipulation
Admin
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..
Admin
"It is known."
Admin
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.
Admin
Admin
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.
Admin
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.
Admin
The real WTF is the amount of incorrect examples of binary operators in the comments.
Admin
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.
Admin
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.
Admin
setBit($val, $bit, '0') will switch bits off, but setBit($val, $bit, 0) will switch them on. Nasty bug.
Admin
No, I take that back.
What the hell kind of WTF has working code?!
Admin
Lesee... comment is wrong... string conversion doesn't account for "nope", "nuh-uh", "no way josé", "hell no", "FileNotFound"...
Admin
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.
Admin
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.
Admin
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.
Admin
"set some strings that humans would consider false...."
Sounds like it has been programmed by a robot....
Admin
oh, I see, you're a troll!
Admin
Admin
Too much learning in higher level language will always ee people unknowing of the truth fundamental.
Admin
Seconded. I just added
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.
Admin
and a troll with 16 fingers too.
Admin
Now there's a WTF if I've ever seen one.
Admin
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.
Admin
MySQL is TRWTF
Admin
WTF. and how, dear sir, do you plan on handling ip addresses without binary ???
CAPTCHA conventio : welcome to conventio, the convention hall planet.
Admin
Admin
Admin
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?
Admin
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.
Admin
What about "Amen", "Insh'allah", "So mote it be" and "Das ist gut, ja?"?
Admin