# Bitwise operators in Javascript.

## Operators to use in program flow.

• All bitwise operators require numeric operands that have integer values​​.
• Javascript is working with floating point numbers, which means that the first step is to convert a floating point to an integer.
• Javascript converts floating point numbers to 32 bits integers and use only the 32 lowest bits where the number to be converted is greater than a 32 bit integer.
• The 32. bit determines whether the integer is positive (0) or negative (1).

Example:
``````<script type="text/javascript">
document.write("<p style='font-family: monospace;' >");
document.write("..........................:");
document.write("   33322222222221111111111000000000<br/>");
document.write("..........................:");
document.write("   21098765432109876543210987654321<br/>");
var num=13689865434.67;
document.write("num = "+num+"<br>");
document.write("written as a binary is ...: "+num.toString(2)+"<br>");
var bitwiseOR=num & 0xffffffff;
document.write("After; num AND 0xffffffff :     "+bitwiseOR.toString(2)+"<br>");
document.write("</p>")
</script>``````
• Here is a list of available bitwise operators in Javascript:
&
(bitwise AND)
Performs a Boolean AND operation on each bit of its integer arguments. A bit is set to 1 if bit in the first operand is 1 AND bit in the second operand is 1, else 0.
``0x0ff0 & 0x00ff evaluates to 0x00f0.``
|
(bitwise OR)
Performs a Boolean OR operation on each bit of its integer arguments. A bit is set to 1 if either bit in the first operand is 1 OR bit in the second operand is 1, else 0.
``0x0ff0 | 0x00ff evaluates to 0x0fff.``
^
(bitwise XOR)
Performs a Boolean exclusive OR operation on each bit of its integer arguments. A bit is set to 1 if the bit in the first operand is different from the bit in the second operand, else 0.
``0x0ff0 | 0x00ff evaluates to 0x0f0f.``
~
(bitwise NOT)
Is a unary operator placed in front of his one integer argument, and operates by reversing all bits of the operand.
`` ~0xff0 evaluates to -ff1.``
<<
(shift left)
The operator moves all bits in its first operand to the left by the number of places specified in the second operand, which should be an integer between 0 and 31.
`` 10 << 1 evaluates to 20. (same as multiplying by 2)``
>>
(shift right with sign )
The operator moves all bits in its first operand to the right by the number of places specified in the second operand, which should be an integer between 0 and 31. Bits that are shifted off the right are lost. If the first operand is negative and at least one of the bit that is lost is not zero then the result will be reduced by one.
`` -0x5A7 >> 3 evaluates to -0x5b. ``
>>>
(shift right with zero fill )
This operator is just like the >> operator, except that the bits shifted in on the left are always zero.
`` -1 evaluates to 0x7fffffff.``
• All bitwise operators, apart from the bitwise NOT, can be combined with an assignment operator as follows:

predefined variable {operator}= expession;

Examples:
``````<script type="text/javascript">
// using  &= comination
var op1=0x0ff0, op2=0x00ff;
op1 &=  op2;               // same as op1=op1 & op2;
document.write("hex: "+op1.toString(16)+" , binary: "+op1.toString(2)+"<br>");

// using  |= comination
var op1=0x0ff0, op2=0x00ff;
op1 |=  op2;               // same as op1=op1 | op2;
document.write("hex: "+op1.toString(16)+" , binary: "+op1.toString(2)+"<br>");

// using  ^= comination
var op1=0x0ff0, op2=0x00ff;
op1 ^=  op2;               // same as op1=op1 ^ op2;
document.write("hex: "+op1.toString(16)+" , binary: "+op1.toString(2)+"<br>");

// using  <<= comination
var op1=10, op2=1;
op1 <<=  op2;               // same as op1=op1 << op2;
document.write("hex: "+op1.toString(16)+" , binary: "+op1.toString(2)+"<br>");

// using  >>= comination
var op1=-0x5A7, op2=3;
op1 >>=  op2;               // same as op1=op1 >> op2;
document.write("hex: "+op1.toString(16)+" , binary: "+op1.toString(2)+"<br>");
</script>``````