douzhan1238 2014-02-23 22:10
浏览 99
已采纳

优先级和位掩码操作

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?

  • 写回答

1条回答 默认 最新

  • duanqian2368 2014-02-23 22:13
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?