// helper function for displaing results var output = function(operator, result) { document.getElementById(operator).innerHTML = result; } // variables var a = 5; var b = 13; // a | b - OR // if in any of the given numbers corresponding bit // is '1', then the result is '1' output('or', a|b); // 13 // a & b - AND // if in both of the given numbers corresponding bit // is '1', then the result is '1' output('and', a&b); // 5 // a ^ b - XOR // if in one of the given numbers (not both) // corresponding bit is '1', then the result is '1' output('xor', a^b); // 8 // ~a - NOT // inverts all the bits output('not', ~a); // -6 // a >> b - RIGHT SHIFT // shift binary representation of 'a' for 'b' // bits to the right, discarding bits shifted off output('rs', a>>b); // 0 // a << b - LEFT SHIFT // shift binary representation of 'a' for 'b' // bits to the right, shifting in zeros from the right output('ls', a<<b); // 40960 // a >>> b - ZERO FILLED RIGHT SHIFT // shift binary representation of 'a' for 'b' // bits to the right, discarding bits shifted off, // and shifting in zeros from the left. output('zfrs', a>>>b); // 0
Result: <p>a | b = <span id="or"></span></p> <p>a & b = <span id="and"></span></p> <p>a ^ b = <span id="xor"></span></p> <p>~ a = <span id="not"></span></p> <p>a >> b = <span id="rs"></span></p> <p>a << b = <span id="ls"></span></p> <p>a >>> b = <span id="zfrs"></span></p>
span { color: #f00; }