Page 155 - JavaScript
P. 155

13 & 7 => 5
         // 13:     0..01101
         //  7:     0..00111
         //-----------------
         //  5:     0..00101 (0 + 0 + 4 + 0 + 1)


        Real world example: Number's Parity Check


        Instead of this "masterpiece" (unfortunately too often seen in many real code parts):


         function isEven(n) {
             return n % 2 == 0;
         }

         function isOdd(n) {
             if (isEven(n)) {
                 return false;
             } else {
                 return true;
             }
         }


        You can check the (integer) number's parity in much more effective and simple manner:


         if(n & 1) {
             console.log("ODD!");
         } else {
             console.log("EVEN!");
         }



        Bitwise OR


        The bitwise OR operation a | b returns the binary value with a 1 where either operands or both
        operands have 1's in a specific position, and 0 when both values have 0 in a position. For example:


         13 | 7 => 15
         // 13:     0..01101
         //  7:     0..00111
         //-----------------
         // 15:     0..01111 (0 + 8 + 4 + 2 + 1)



        Bitwise NOT



        The bitwise NOT operation ~a flips the bits of the given value a. This means all the 1's will become
        0's and all the 0's will become 1's.


         ~13 => -14
         // 13:     0..01101
         //-----------------
         //-14:     1..10010 (-16 + 0 + 0 + 2 + 0)






        https://riptutorial.com/                                                                             112
   150   151   152   153   154   155   156   157   158   159   160