JavaScript provides us with bitwise operators to perform bit manipulation operations on numbers.
While carrying out bitwise operations, the interpreter performs a sequence of steps.
Below we can see the steps listed in the order that is followed.
Convert to binary: When a bitwise operation is performed, the operands are converted to their equivalent 32-bit signed binary integer.
Operator result: After the conversion, the bitwise operation is performed, and a 32-bit result is produced.
Convert back: The result value is then converted to its equivalent value and returned by the operator.
Below is a table that shows JavaScript's different types of bitwise operators.
Operator | Description |
& | AND |
| | OR |
~ | NOT |
^ | XOR |
<< | Left shift |
>> | Right shift |
>>> | Unsigned right shift |
Below is a JavaScript application showing how the different bitwise operators work.
In the application, we provide two numbers, num1
and num2
. We perform all the operations listed in the table above on these numbers.
const num1 = 15const num2 = 8const num1b = num1.toString(2)const num2b = num2.toString(2)const and = num1 & num2console.log("AND Operation: ")console.log(num1 + " & " + num2 + " = " + num1b + " & " + num2b + " = " + and.toString(2) + " = " + and)console.log("\n")const or = num1 | num2console.log("OR Operation: ")console.log(num1 + " | " + num2 + " = " + num1b + " | " + num2b + " = " + or.toString(2) + " = " + or)console.log("\n")const xor = num1 ^ num2console.log("XOR Operation: ")console.log(num1 + " ^ " + num2 + " = " + num1b + " ^ " + num2b + " = " + xor.toString(2) + " = " + xor)console.log("\n")const not = ~num2console.log("NOT Operation: ")console.log("~" + num2 + " = " + "~" + num2b + " = " + not.toString(2) + " = " + not)console.log("\n")const leftShift = num2 << 1console.log("Left Shift Operation: ")console.log(num2 + " << " + 1 + " = " + num2b + " << " + 1 + " = " + leftShift.toString(2) + " = " + leftShift)console.log("\n")const rightShift = num2 >> 1console.log("Right Shift Operation: ")console.log(num2 + " >> " + 1 + " = " + num2b + " >> " + 1 + " = " + rightShift.toString(2) + " = " + rightShift)console.log("\n")const rightShiftUnsigned = num2 >>> 1console.log("Unsigned Right Shift Operation: ")console.log(num2 + " >>> " + 1 + " = " + num2b + " >>> " + 1 + " = " + rightShiftUnsigned.toString(2) + " = " + rightShiftUnsigned)console.log("\n")
Lines 1–2: We initialize the two numbers num1
and num2
on which we are to perform bitwise operations.
Lines 4–5: The numbers are converted to binary strings using the toString()
function by passing the argument 2
.
Lines 7–10: Perform the AND (&
) operation on num1
and num2
, and display the result.
Lines 12–15: Perform the OR (|
) operation on num1
and num2
, and display the result.
Lines 17–20: Perform the XOR (^
) operation on num1
and num2
, and display the result.
Lines 22–25: Perform the NOT (~
) operation on num2
, and display the result.
Lines 27–30: Perform the left shift (<<
) operation on num2
with a shift value of 1
and show the result.
Lines 32–35: Perform the right shift (>>
) operation on num2
with a shift value of 1
and show the result.
Lines 37–40: Perform the unsigned right shift (>>>
) operation on num2
with a shift value of 1
and show the result.
Free Resources