JavaScript is a popular programming language used for creating interactive web pages. It is a high-level language that is easy to learn and use. One of the most powerful features of JavaScript is its ability to perform bitwise operations. Bitwise operations are used to manipulate the individual bits of a number. This can be useful in a variety of applications, such as cryptography, compression, and data storage.
Bitwise operations are performed on binary numbers, which are numbers expressed in base 2. In binary, each digit can only be 0 or 1. Bitwise operations allow you to manipulate the individual bits of a binary number. There are six bitwise operators in JavaScript:
The AND operator (&) returns a 1 in each bit position where both operands have a 1. The OR operator (|) returns a 1 in each bit position where either operand has a 1. The XOR operator (^) returns a 1 in each bit position where only one operand has a 1. The NOT operator (~) returns the complement of the operand, which means it flips all the bits. The left shift operator (<<) shifts the bits to the left by a specified number of positions, and the right shift operator (>>) shifts the bits to the right by a specified number of positions.
Here are some examples of how to use bitwise operators in JavaScript:
The following code demonstrates the AND operator:
var a = 5; // 0101
var b = 3; // 0011
var c = a & b; // 0001
console.log(c); // 1
In this example, the AND operator is used to compare the binary values of a and b. The result is a binary number where each bit position contains a 1 only if both operands have a 1 in that position. In this case, the result is 0001, which is equivalent to the decimal value 1.
The following code demonstrates the OR operator:
var a = 5; // 0101
var b = 3; // 0011
var c = a | b; // 0111
console.log(c); // 7
In this example, the OR operator is used to compare the binary values of a and b. The result is a binary number where each bit position contains a 1 if either operand has a 1 in that position. In this case, the result is 0111, which is equivalent to the decimal value 7.
The following code demonstrates the XOR operator:
var a = 5; // 0101
var b = 3; // 0011
var c = a ^ b; // 0110
console.log(c); // 6
In this example, the XOR operator is used to compare the binary values of a and b. The result is a binary number where each bit position contains a 1 only if one operand has a 1 in that position. In this case, the result is 0110, which is equivalent to the decimal value 6.
The following code demonstrates the NOT operator:
var a = 5; // 0101
var b = ~a; // 1010
console.log(b); // -6
In this example, the NOT operator is used to flip all the bits in the binary value of a. The result is a binary number where each bit position contains the opposite value of the original operand. In this case, the result is 1010, which is equivalent to the decimal value -6. This is because JavaScript uses two's complement notation to represent negative numbers.
The following code demonstrates the left shift operator:
var a = 5; // 0101
var b = a << 2; // 10100
console.log(b); // 20
In this example, the left shift operator is used to shift the bits of a to the left by two positions. This effectively multiplies the value of a by 2 to the power of the shift amount. In this case, the result is 10100, which is equivalent to the decimal value 20.
The following code demonstrates the right shift operator:
var a = 5; // 0101
var b = a >> 1; // 0010
console.log(b); // 2
In this example, the right shift operator is used to shift the bits of a to the right by one position. This effectively divides the value of a by 2 to the power of the shift amount. In this case, the result is 0010, which is equivalent to the decimal value 2.