| « Prev | Page 1 | Page 2 | Page 3 | Next » |
Re: The Bit Setter
2012-05-08 09:47
•
by
werner
(unregistered)
|
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 ;) |
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. |
|
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. |
You've gotta be kidding. |
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? |
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. |
|
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'? |
Re: The Bit Setter
2012-05-08 19:09
•
by
Puzzles
(unregistered)
|
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. |
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. |
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. |
Re: The Bit Setter
2012-05-09 00:33
•
by
Shinobu
(unregistered)
|
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)Which could be a slight improvement over this: on(checkbox.click)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. |
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 ? |
Re: The Bit Setter
2012-05-09 09:29
•
by
Apologist
(unregistered)
|
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. |
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 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 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 |
|
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.
|
Re: The Bit Setter
2012-05-13 07:47
•
by
PunctuallyChallenged
(unregistered)
|
|
We're looking forward to reading your code in an upcoming TDWTF!
|
|
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!
|
Re: The Bit Setter
2012-05-20 10:00
•
by
History Teacher
(unregistered)
|
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 ;-). |
|
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... |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |