callback function return return($var & 1)?

前端 未结 8 2128
灰色年华
灰色年华 2020-12-05 22:07

I have read the PHP Manuel about array_filter



        
8条回答
  •  不知归路
    2020-12-05 22:46

    You know && is AND, but what you probably don't know is & is a bit-wise AND.

    The & operator works at a bit level, it is bit-wise. You need to think in terms of the binary representations of the operands.

    e.g.

    710 & 210 = 1112 & 0102 = 0102 = 210

    For instance, the expression $var & 1 is used to test if the least significant bit is 1 or 0, odd or even respectively.

    $var & 1

    010 & 110 = 0002 & 0012 = 0002 = 010 = false (even)

    110 & 110 = 0012 & 0012 = 0012 = 110 = true  (odd)

    210 & 110 = 0102 & 0012 = 0002 = 010 = false (even)

    310 & 110 = 0112 & 0012 = 0012 = 110 = true  (odd)

    410 & 210 = 1002 & 0012 = 0002 = 010 = false (even)

    and so on...

提交回复
热议问题