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]
« PrevPage 1 | Page 2 | Page 3Next »

Re: The Bit Setter

2012-05-07 08:11 • by ParkinT
$newComment = $comment && ( ~ $first);

Re: The Bit Setter

2012-05-07 09:03 • by pjt33
In what way "doesn't [it] quite work as advertised"?

Re: The Bit Setter

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

Re: The Bit Setter

2012-05-07 09:10 • by Black Bart (unregistered)
380451 in reply to 380450
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.

Re: The Bit Setter

2012-05-07 09:19 • by flob9 (unregistered)
380452 in reply to 380449
"bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal

Re: The Bit Setter

2012-05-07 09:22 • by Rootbeer
380453 in reply to 380452

Isn't it conventional for the least significant bit to be enumerated as 0?

A byte with bit 4 set should be %00010000.

Re: The Bit Setter

2012-05-07 09:24 • by Bert (unregistered)
380454 in reply to 380449
There's no bit shifting involved. So if you wanted to set the 7th bit, you'd call
setBit($val, 7)
and it would return
$val | 7
, which is not setting the 7th bit (it is setting the first 3 bits).

Re: The Bit Setter

2012-05-07 09:26 • by ¯\(°_o)/¯ I DUNNO LOL (unregistered)
// set some strings that humans would consider false
// but would be converted to true if converted by PHP
Ah, there's TRWTF.

Re: The Bit Setter

2012-05-07 09:27 • by RedFox (unregistered)
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:

if ($switch)
$newval = $val | (1<<$bit); // bit on
else
$newval = $val & ~(1<<$bit); // bit off

Re: The Bit Setter

2012-05-07 09:32 • by flob9 (unregistered)
//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];

Re: The Bit Setter

2012-05-07 09:33 • by tom103 (unregistered)
TRWTF is PHP...

Re: The Bit Setter

2012-05-07 09:35 • by Arild Kvalbein (unregistered)
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)

Re: The Bit Setter

2012-05-07 09:45 • by Silent D (unregistered)
My bits are not getting turned on by this code.

Re: The Bit Setter

2012-05-07 09:48 • by flob9 (unregistered)
380461 in reply to 380459
@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)

Re: The Bit Setter

2012-05-07 09:59 • by 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.

Re: The Bit Setter

2012-05-07 10:04 • by 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...

Re: The Bit Setter

2012-05-07 10:04 • by QD (unregistered)
380465 in reply to 380463
Maybe a third one will do the trick :)

Re: The Bit Setter

2012-05-07 10:06 • by simonbuchan (unregistered)
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?

Re: The Bit Setter

2012-05-07 10:07 • by vt_mruhlin (unregistered)
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.

Re: The Bit Setter

2012-05-07 10:07 • by redtetrahedron
380468 in reply to 380464
12 = 1100
05 = 0101
13 = 1101

Re: The Bit Setter

2012-05-07 10:10 • by vt_mruhlin (unregistered)
380469 in reply to 380464
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...


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?

Re: The Bit Setter

2012-05-07 10:12 • by vt_mruhlin (unregistered)
380470 in reply to 380468
redtetrahedron:
12 = 1100
05 = 0101
13 = 1101


I need more coffee before I just start agreeing with people.

Re: The Bit Setter

2012-05-07 10:16 • by Pablo (unregistered)
380471 in reply to 380464
Not sure how you're converting decimal to binary or vice versa:

12 = b1100 (1*8 + 1*4 + 0* 2 + 0*1)
5 = b101 (you had this right: 1*4 + 0*2 + 1*1)

12 | 5 = b1100 | b0101 = b1101 = (1*8 + 1*4 + 0*2 + 1*1) = 13

also, 17 would be 10001 (1 * 16 + 0*8 + 0*4 + 0*2 + 1 * 1)

Re: The Bit Setter

2012-05-07 10:21 • by Sarten X (unregistered)
380472 in reply to 380449

// set some strings that humans would consider false
// but would be converted to true if converted by PHP
if (is_string($switch)) {
switch ($switch) {
case 'false' :
case 'down' :
case 'off' :
case 'not' :
case '0' :
case '' :

Obviously, this would only be considered working if it accounted for "FileNotFound".

Re: The Bit Setter

2012-05-07 10:21 • by 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. :(

Addendum (2012-05-07 10:27):

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

Re: The Bit Setter

2012-05-07 10:27 • by KJ (unregistered)
380474 in reply to 380459
This was my impression. The code looks like it would work fine to me.

Re: The Bit Setter

2012-05-07 10:28 • by redtetrahedron
380475 in reply to 380454
Bert:
There's no bit shifting involved. So if you wanted to set the 7th bit, you'd call
setBit($val, 7)
and it would return
$val | 7
, which is not setting the 7th bit (it is setting the first 3 bits).


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.

Re: The Bit Setter

2012-05-07 10:40 • by dargor17
380476 in reply to 380471
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.

Re: The Bit Setter

2012-05-07 10:56 • by ¯\(°_o)/¯ I DUNNO LOL (unregistered)
380477 in reply to 380463
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.
I would start with its very existence as being the first WTF. This is functionizing a basic feature of the language, and doing it in a bad way.

Re: The Bit Setter

2012-05-07 11:02 • by AGray (unregistered)
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

Re: The Bit Setter

2012-05-07 11:07 • by JAPH (unregistered)
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.

Re: The Bit Setter

2012-05-07 11:12 • by PHPer (unregistered)
As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.

Re: The Bit Setter

2012-05-07 11:16 • by lanzz (unregistered)
380481 in reply to 380450
here's a very php bitwise operation example:

error_reporting((E_ALL | E_STRICT) & ~E_DEPRECATED)

Re: The Bit Setter

2012-05-07 11:22 • by null (unregistered)
380482 in reply to 380464
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...

I'm actually puzzled at how you think 12 is 10010, 13 is 10011, or that 17 is 10111? Suggestion: if you failed CS 101, don't bother posting comments.

Re: The Bit Setter

2012-05-07 11:31 • by sully (unregistered)
380483 in reply to 380464
Check your numbers. 12 is 1100 and 13 is 1101. 17 is 10001. You got 5 right, though.

Re: The Bit Setter

2012-05-07 11:34 • by sully (unregistered)
380484 in reply to 380480
As far as I know 'false' and '0' would be correctly evaluated to FALSE by PHP.


"Correctly". In no sense is doing that *correct*!

Re: The Bit Setter

2012-05-07 11:39 • by Paul Neumann (unregistered)
380485 in reply to 380469
vt_mruhlin:
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...


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

Re: The Bit Setter

2012-05-07 11:48 • by Tyler (unregistered)
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.

Re: The Bit Setter

2012-05-07 12:03 • by Maurits
380487 in reply to 380464
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)

Re: The Bit Setter

2012-05-07 12:06 • by Ben Jammin (unregistered)
380488 in reply to 380478
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.

Re: The Bit Setter

2012-05-07 12:11 • by Evan (unregistered)
380489 in reply to 380452
flob9:
"bit 4" doesn't mean 4 in decimal, it means the 4th bit in binary (1000) == 8 in decimal

Interestingly enough the code does not use the term "bit 4".

Re: The Bit Setter

2012-05-07 12:20 • by Jerry (unregistered)
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!

Re: The Bit Setter

2012-05-07 12:28 • by Fred (unregistered)
case 'false' :
case 'down' :
case 'off' :
case 'not' :
case '0' :
case '' :
$switch = false;

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?

default :
$switch = true;

Oh, yeah, I guess it can.

Re: The Bit Setter

2012-05-07 12:31 • by xtremezone
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).

Re: The Bit Setter

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

Re: The Bit Setter

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

Re: The Bit Setter

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

Re: The Bit Setter

2012-05-07 12:52 • by redtetrahedron
380496 in reply to 380477
¯\(°_o)/¯ I DUNNO LOL:
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.
I would start with its very existence as being the first WTF. This is functionizing a basic feature of the language, and doing it in a bad way.


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.

Re: The Bit Setter

2012-05-07 12:53 • by gnomie (unregistered)
380497 in reply to 380487
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).

Re: The Bit Setter

2012-05-07 12:53 • by Fonseca (unregistered)
380498 in reply to 380482
Got it! It's BCD! :))

0000.0101 = 5
0001.0010 = 12
0001.0111 = 17

you're more of an electronics guy, aren't you?
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment