Bitwise '&' operator

前端 未结 6 982
慢半拍i
慢半拍i 2020-11-28 14:43

I am lacking some basic understanding in bitwise \'&\' operator.

5 = 101
4 = 100

So why the output of the below if conditi

6条回答
  •  [愿得一人]
    2020-11-28 15:24

    Understanding bitwise operator truth tables is crucial. Consider the following, where A and B are inputs and Y is the output.

    & (Bitwise And) When inputs A and B are true, output is true; otherwise output is false

    A   B   Y
    ---------
    0 | 0 | 0
    0 | 1 | 0
    1 | 0 | 0
    1 | 1 | 1
    

    | (Bitwise Or) When A or B or both inputs are true output is true; otherwise output is false

    A   B   Y
    ---------
    0 | 0 | 0
    0 | 1 | 1
    1 | 0 | 1
    1 | 1 | 1
    

    ^ (Bitwise X-Or) When A and B are opposite states, output is true; otherwise output is false

    A   B   Y
    ---------
    0 | 0 | 0
    0 | 1 | 1
    1 | 0 | 1
    1 | 1 | 0
    

    ! (Bitwise Not) Output is the opposite state of the input

    A   Y
    -----
    0 | 1
    1 | 0
    

    Your Equation (5 & 4) == (0101 & 0100) == 0100 == 4 == true

      0101
    & 0100
    ------
      0100
    

提交回复
热议问题