Boolean operators vs Bitwise operators

前端 未结 9 1231
孤街浪徒
孤街浪徒 2020-11-22 06:34

I am confused as to when I should use Boolean vs bitwise operators

  • and vs &
  • or vs |
9条回答
  •  太阳男子
    2020-11-22 07:28

    Logical Operations

    are usually used for conditional statements. For example:

    if a==2 and b>10:
        # Do something ...
    

    It means if both conditions (a==2 and b>10) are true at the same time then the conditional statement body can be executed.

    Bitwise Operations

    are used for data manipulation and extraction. For example, if you want to extract the four LSB (Least Significant Bits) of an integer, you can do this:

    p & 0xF
    

提交回复
热议问题