 # Lession - #236 Javascript Bitwise

## JavaScript Bitwise Operators

The JavaScript Bitwise Operators are demonstrated below.

Example:
``````<script>
var a = 4;
var b = 1;

document.write("A & B = " + (a & b> + '<br>'>;

document.write("A | B = " + (a | b> + '<br>'>;

document.write("~A = " + (~a> + '<br>'>;
</script>``````

Output: Try it here

JavaScript, like C, C++, Java, Python, and a variety of other languages, allows bit-wise operations. A number is stored as a 64-bit floating-point number in JavaScript, but the bit-wise operation is performed on a 32-bit binary number, i.e. to execute a bit-operation on a 32-bit binary number. JavaScript turns the number to a signed 32-bit binary number, performs the operation, and then converts the result back to a 64-bit number.
A few bit-wise logical operators used in JavaScript are listed below.

The binary AND (&>
operator accepts two operands and is a binary operator. If both bits are set (i.e. 1>
, bit-wise AND (&>
yields 1; otherwise, it returns 0.

ABOUTPUT ( A & B >
000
010
100
111

The binary operator Bit-Wise OR (|>
accepts two operands and is a binary operator. If any of the operands is set (i.e. 1>
, bit-wise OR (|>
yields 1; otherwise, it returns 0.

ABOUTPUT ( A | B >
000
011
101
111

Bit-Wise XOR (>
accepts two operands and is a binary operator. If both operands are different, bit-wise XOR (>
yields 1; otherwise, it returns 0.

ABOUTPUT ( A ^ B >
000
011
101
110

Bit-Wise NOT (>
is a unary operator, which means it only accepts one operand. NOT (>
flips the bits in a bit-wise manner, i.e. 0 becomes 1 and 1 becomes 0.

AOUTPUT ( ~A >
01
10

Shift to the left (<<>
:
It's a binary operator, which means it only takes two operands. The number is specified by the first operator, and the number of bits to shift is specified by the second operator. Each bit is shifted to the left, and 0 bits from the right are added. Excess parts from the left are thrown away.

A 6 ( 00000000000000000000000000000110 > 1 ( 00000000000000000000000000000001 > 12 ( 00000000000000000000000000001100 >

Sign Propagating Right Shift ( >> >
:
Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bit is shifted towards the right, the overflowing bits are discarded. This is Sign Propagating as the bits which are added from the left depends upon the sign of the number (i.e. 0 if positive and 1 if negative >

 A 6 ( 00000000000000000000000000000110 > 1 ( 00000000000000000000000000000001 > 3 ( 00000000000000000000000000000011 >

Zero fill Right Shift ( >>> >
:
Its a binary operator i.e. it accepts two operand. The first operand specifies the number and the second operand specifies the number of bits to shift. Each bits is shifted towards right, the overflowing bits are discarded. 0 bit is added from the left so its zero fill right shift.

A 6 ( 00000000000000000000000000000110 > 1 ( 00000000000000000000000000000001 > 3 ( 00000000000000000000000000000011 >

The above-mentioned operators are implemented as follows.

``````<h3>JavaScript Bitwise Operators.</h3>

<script>
var a = 8;
var b = 2;

// AND Operation
document.write("A & B = " + (a & b> + '<br>'>;

// OR operation
document.write("A | B = " + (a | b> + '<br>'>;

// NOT operation
document.write("~A = " + (~a> + '<br>'>;

// Sign Propagating Right Shift
document.write("A >> B = " + (a >> b> + '<br>'>;

// Zero Fill Right Shift
document.write("A >>> B = " + (a >>> b> + '<br>'>;

// Left Shift
document.write("A << B = " + (a << b> + '<br>'>;
</script>``````

Output: Try it here