# Comment On The Bit Setter

"Bit manipulation can be tricky," writes Nathan, "especially if you have no familiarity with bitwise operators or logic." [expand full text]
 « Prev Page 1 | Page 2 | Page 3 Next »

### Re: The Bit Setter

2012-05-07 12:54 • by Overthinker (unregistered)
 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, ...

### Re: The Bit Setter

2012-05-07 12:54 • by gnomie (unregistered)
 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...

### Re: The Bit Setter

2012-05-07 12:56 • by Fonseca (unregistered)
 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)

### Re: The Bit Setter

2012-05-07 13:20 • by xtremezone
 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...

### Re: The Bit Setter

2012-05-07 13:22 • by Jack (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 13:35 • by 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...

### Re: The Bit Setter

2012-05-07 14:11 • by 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"?

### Re: The Bit Setter

2012-05-07 14:15 • by Nagesh
 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.

### Re: The Bit Setter

2012-05-07 14:18 • by 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?

### Re: The Bit Setter

2012-05-07 14:34 • by Lurch (unregistered)
 "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).

### Re: The Bit Setter

2012-05-07 14:36 • by flob9 (unregistered)
 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++)

### Re: The Bit Setter

2012-05-07 14:56 • by Paul (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 15:01 • by Jodo (unregistered)
 lol, true that... :)

### Re: The Bit Setter

2012-05-07 15:04 • by Paul (unregistered)
 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'...?

### Re: The Bit Setter

2012-05-07 15:23 • by fomori (unregistered)
 "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) ...

### Re: The Bit Setter

2012-05-07 16:16 • by AGray (unregistered)
 Oh. My AND was wrong. Sorry about that. :| I am TRWTF. :( Captcha: Ullamcorper Don't even ask me what that's supposed to be...

### Re: The Bit Setter

2012-05-07 16:18 • by Strolskon
 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.

### Re: The Bit Setter

2012-05-07 16:25 • by Evan (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 17:28 • by Your Name (unregistered)
 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

### Re: The Bit Setter

2012-05-07 17:41 • by Rabble (unregistered)
 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..

### Re: The Bit Setter

2012-05-07 17:53 • by Jack (unregistered)
 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."

### Re: The Bit Setter

2012-05-07 18:20 • by Davey Jones (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 18:21 • by null (unregistered)
 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."

### Re: The Bit Setter

2012-05-07 18:22 • by TheReal(TM) Anonymous (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 19:02 • by Coyne
 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.

### Re: The Bit Setter

2012-05-07 19:26 • by ydrol (unregistered)
 The real WTF is the amount of incorrect examples of binary operators in the comments.

### Re: The Bit Setter

2012-05-07 19:34 • by AN AWESOME CODER (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 19:40 • by AN AWESOME CODER (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 20:06 • by Meep (unregistered)
 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.

### Re: The Bit Setter

2012-05-07 20:08 • by Meep (unregistered)
 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?!

### Re: The Bit Setter

2012-05-07 22:30 • by lolwtf
 Lesee... comment is wrong... string conversion doesn't account for "nope", "nuh-uh", "no way josé", "hell no", "FileNotFound"...

### Re: The Bit Setter

2012-05-08 01:09 • by hikari
 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.

### Re: The Bit Setter

2012-05-08 01:28 • by Gibbon1 (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 01:37 • by Dick Head (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 02:07 • by Raven
 "set some strings that humans would consider false...." Sounds like it has been programmed by a robot....

### Re: The Bit Setter

2012-05-08 02:56 • by sa (unregistered)
 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!

### Re: The Bit Setter

2012-05-08 03:04 • by Captcha:genitus (unregistered)
 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

### Re: The Bit Setter

2012-05-08 03:06 • by dxf (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 03:27 • by werner (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 03:54 • by peter (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 04:19 • by toon (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 05:42 • by L. (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 05:45 • by L. (unregistered)
 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

### Re: The Bit Setter

2012-05-08 05:52 • by L. (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 06:01 • by bitbitbit (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 06:01 • by 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??

### Re: The Bit Setter

2012-05-08 08:04 • by Carl (unregistered)
 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?

### Re: The Bit Setter

2012-05-08 08:36 • by L. (unregistered)
 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.

### Re: The Bit Setter

2012-05-08 09:22 • by QJo
 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?"?