What are bitwise operators?

后端 未结 9 1971
闹比i
闹比i 2020-11-22 02:05

I\'m someone who writes code just for fun and haven\'t really delved into it in either an academic or professional setting, so stuff like these bitwise operators really esca

9条回答
  •  醉梦人生
    2020-11-22 02:37

    It is worth noting that the single-bit truth tables listed as other answers work on only one or two input bits at a time. What happens when you use integers, such as:

    int x = 5 & 6;
    

    The answer lies in the binary expansion of each input:

      5 = 0 0 0 0 0 1 0 1
    & 6 = 0 0 0 0 0 1 1 0
    ---------------------
          0 0 0 0 0 1 0 0
    

    Each pair of bits in each column is run through the "AND" function to give the corresponding output bit on the bottom line. So the answer to the above expression is 4. The CPU has done (in this example) 8 separate "AND" operations in parallel, one for each column.

    I mention this because I still remember having this "AHA!" moment when I learned about this many years ago.

提交回复
热议问题