js Bitwise Operators
snippet in javascript

js Bitwise Operators

user6946

Javascript Bitwise Operators
& AND statement
| OR statement
~ NOT
^ XOR
<< Left shift
>> Right shift
>>> Zero fill right shift

Bitwise Operators js

user5379

Converting Decimal to Binary
function dec2bin(dec){

  return (dec >>> 0).toString(2);

}
 

Bitwise Operators js

user7943

This is a sign preserving right shift. Copies of the leftmost bit are pushed in from the left, and the rightmost bits fall off:
var x = -5 >> 1;
 

javascript bitwise flags

user2545

var myEnum = {
  left: 1,
  right: 2,
  top: 4,
  bottom: 8
}

var myConfig = myEnum.left | myEnum.right;

if (myConfig & myEnum.right) {
  // right flag is set
}

Bitwise Operators js

user8732

Converting Binary to Decimal
function bin2dec(bin){

    return parseInt(bin, 2).toString(10);

}
 

Bitwise Operators js

user7059

Bitwise AND returns 1 only if both bits are 1:
var x = 5 & 1;