94

I've come across a (seemingly) very strange case.

Take the number 2 (0b10) and bitmask it with 1 (0b01)

This should produce 0b00 which is equivalent to 0.

However, here's where Mr Schrödinger comes in:

var_dump(0b10 & 0b01); // int(0)
var_dump(0b10 & 0b01 == 0); // int(0)
var_dump(0b10 & 0b01 != 0); // int(0)

Whiskey. Tango. Foxtrot.

I am, admittedly, not the sharpest when it comes to bitwise operators - so maybe I've got horribly, horribly wrong somewhere?

However, in Python:

0b10 & 0b01 == 0 = True

0b10 & 0b01 != 0 = False

...so?

Wolf
  • 9,679
  • 7
  • 62
  • 108
Danny Kopping
  • 4,862
  • 2
  • 29
  • 38

1 Answers1

150

You are actually doing this:

var_dump(0b10 & (0b01 == 0));
var_dump(0b10 & (0b01 != 0));

Try:

var_dump((0b10 & 0b01) == 0);
var_dump((0b10 & 0b01) != 0);
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • 6
    Seems to me that PHP has strnage operator precedence overall. – Alvin Wong Feb 24 '14 at 10:11
  • 23
    I guess that's why I **NEVER** rely on precendence in any language. Moreover, I think parentheses make code more readable. In extreme cases you might want to (re-)group and short-comment stuff. – No answer Feb 24 '14 at 10:35
  • 2
    @AlvinWong You are correct! I think rarely a code need conditional expression like `0b10 & (0b01 == 0)` why would someone apply bitwise with yes no kind of information. – Grijesh Chauhan Feb 24 '14 at 10:35
  • 4
    Well, consider a more common example: the ternary operator, PHP has it backwards, making it different than all other languages. Ref: phpsadness – Alvin Wong Feb 24 '14 at 10:38
  • 5
    Thats why in C and C++ people have warnings for these things. – PlasmaHH Feb 24 '14 at 11:43
  • @AlvinWong: To be fair, this particular case is equivalent to the precedence in most C-like languages... – Oliver Charlesworth Feb 24 '14 at 12:37
  • Of course, what else than precedence, programming isn't quantum physics. Still +1 for the obviously correct answer to an awfully overrated question. – Christian Rau Feb 24 '14 at 12:59
  • 1
    C-like precedence for `&` and `|` is unnatural, and an unfortunate side-effect of C's evolution from 'B'. See the 'Neonatal C' section of Ritchie's [The Development of the C Language](http://cm.bell-labs.com/cm/cs/who/dmr/chist.html) – AShelly Feb 24 '14 at 16:36
  • @ChristianRau: It seems the question does show research, is clear, and is useful. What more could you ask for that a lack makes this question overrated? – AlbeyAmakiir Feb 25 '14 at 04:45
  • I rely on the precedence rules that match what we learned in grade school arithmetic (e.g. multiplication is higher than addition), and a few other common cases. For most expressions more complicated than that I parenthesize liberally. – Barmar Feb 25 '14 at 18:39
  • @AlbeyAmakiir The question isn't bad, don't get me wrong, but I still fear most of the up-votes are mainly due to people nerdgasming on the title (which has been changed already, though). But nevermind, it's not that I would care that much (and it may also be such a large number of votes is usual in the PHP area of the site, don't know). And well, what else could have been the problem but precedence, but I don't blame a newbie for not immediately getting that and the question isn't bad. – Christian Rau Feb 26 '14 at 10:10