When should I use a bitwise operator?

后端 未结 9 871
暗喜
暗喜 2020-12-02 14:01

I read the following Stack Overflow questions, and I understand the differences between bitwise and logical.

  • Difference between & and && in

9条回答
  •  攒了一身酷
    2020-12-02 14:27

    In most cases, you'll probably want to use logical operators. They're used for combining logical conditions, generally to control program flow, e.g. ($isAlive && $wantsToEat).

    Bitwise operators are used when you want to perform operations on a bit-by-bit basis on the underlying binary representations of integers. e.g. (5 & 3) == 7. As others have suggested, there's usually not a lot of call for this in the sort of application that tends to get written in PHP (although there is in lower-level languages, like C).

提交回复
热议问题