What are bitwise operators?

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

    I kept hearing about how slow JavaScript bitwise operators were. I did some tests for my latest blog post and found out they were 40% to 80% faster than the arithmetic alternative in several tests. Perhaps they used to be slow. In modern browsers, I love them.

    I have one case in my code that will be faster and easier to read because of this. I'll keep my eyes open for more.

    0 讨论(0)
  • 2020-11-22 02:34

    These are the bitwise operators, all supported in JavaScript:

    • op1 & op2 -- The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.

    • op1 | op2 -- The OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.

    • op1 ^ op2 -- The EXCLUSIVE-OR operator compares two bits and returns 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1.

    • ~op1 -- The COMPLEMENT operator is used to invert all of the bits of the operand.

    • op1 << op2 -- The SHIFT LEFT operator moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. Each move to the left effectively multiplies op1 by 2.

    • op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half. The left-most sign bit is preserved.

    • op1 >>> op2 -- The SHIFT RIGHT - ZERO FILL operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half. The left-most sign bit is discarded.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 02:41

    To break it down a bit more, it has a lot to do with the binary representation of the value in question.

    For example (in decimal):
    x = 8
    y = 1
    
    would come out to (in binary):
    x = 1000
    y = 0001
    
    From there, you can do computational operations such as 'and' or 'or'; in this case:
    x | y = 
    1000 
    0001 |
    ------
    1001
    
    or...9 in decimal
    

    Hope this helps.

    0 讨论(0)
  • 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:

    <html>
    <body>
    <script>
        alert("\"Cat\" && \"Dog\" = " + ("Cat" && "Dog") + "\n"
            + "2 && 4 = " + (2 && 4) + "\n"
            + "2 & 4 = " + (2 & 4));
    </script>
    
    0 讨论(0)
  • 2020-11-22 02:44

    It might help to think of it this way. This is how AND (&) works:

    It basically says are both of these numbers ones, so if you have two numbers 5 and 3 they will be converted into binary and the computer will think

             5: 00000101
             3: 00000011
    

    are both one: 00000001 0 is false, 1 is true

    So the AND of 5 and 3 is one. The OR (|) operator does the same thing except only one of the numbers must be one to output 1, not both.

    0 讨论(0)
提交回复
热议问题