What are bitwise operators?

后端 未结 9 1776
闹比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:43

    When the term "bitwise" is mentioned, it is sometimes clarifying that is is not a "logical" operator.

    For example in JavaScript, bitwise operators treat their operands as a sequence of 32 bits (zeros and ones); meanwhile, logical operators are typically used with Boolean (logical) values but can work with non-Boolean types.

    Take expr1 && expr2 for example.

    Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

    a = "Cat" && "Dog"     // t && t returns Dog
    a = 2 && 4     // t && t returns 4
    

    As others have noted, 2 & 4 is a bitwise AND, so it will return 0.

    You can copy the following to test.html or something and test:

    
    
    
    

提交回复
热议问题