Javascript 简明教程

JavaScript - Bitwise Operators

JavaScript Bitwise Operators

JavaScript 中的按位运算符在二进制级别对整数值执行操作。它们用于操作整数值的各个位。按位运算符类似于逻辑运算符,但它们对单个位起作用。

The bitwise operators in JavaScript perform operations on the integer values at the binary level. They are used to manipulate each bit of the integer values. Bitwise operators are similar to logical operators but they work on individual bits.

JavaScript 按位运算符对 32 位操作数起作用。在 JavaScript 中,数字以 64 位浮点数形式存储。JavaScript 在执行操作之前将数字转换为 32 位带符号整数。在按位操作之后,它将结果转换为 64 位数字。

JavaScript bitwise operators works on 32-bits operands. In JavaScript, numbers are stored as 64-bit floating point number. JavaScript converts the numbers to 32-bit signed integer before performing the operation. After bitwise operation, it converts the result to 64-bits number.

JavaScript 中有七个按位运算符。以下是包含描述的按位运算符列表。

There are seven bitwise operators in JavaScript. Following is the list of bitwise operators with description.

Operator

Name

Description

&

Bitwise AND

Returns 1 if both bits are 1, otherwise 0.

Bitwise OR

Returns 1 if either bit is 1, otherwise 0.

^

Bitwise XOR

Returns 1 if both bits are different, otherwise 0.

!

Bitwise NOT

Returns 1 if bit is 0, otherwise 0.

<<

Left Shift

Shifts the bits left by pushing zeros in from right and discarding leftmost bits.

>>

Right Shift

Shifts the bits right by pushing copies of leftmost bit in from left and discarding rightmost bits.

>>>

Right Shift with Zero

JavaScript Bitwise AND (&) Operator

bitwise AND (&) 运算符对其整数运算符的每一对位执行 AND 运算。运算后,它返回一个更新了位的新的整数。

The bitwise AND (&) operator performs AND operation on each pair of bits of its integer operands. After the operation, it returns a new integer value with the updated bits.

当按位 AND 运算符应用于一对位时,如果两个位都是 1,则返回 1,否则返回 0。

When bitwise AND operator is applied on a pair of bits, it returns 1 if both bits are 1, otherwise returns 0.

以下是按位 AND 运算的真值表 −

Following is the truth table for bitwise AND operation −

A

B

A & B

0

0

0

0

1

0

1

0

0

1

1

1

让我们以 4 位运算符为例了解按位 AND 运算。

Let’s understand bitwise AND operation taking an example of 4-bit operands.

A

B

A & B

1111

0001

0001

1111

0010

0010

1111

0100

0100

1111

1000

1000

Example

让我们对 5 和 7 执行按位 AND (&) 运算。这些数字表示为 32 位整数。

Let’s perform bitwise AND (&) operation on 5 and 7. These numbers are represented as 32-bits integer.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

7

00000000000000000000000000000111

5 & 7

00000000000000000000000000000101 (= 5)

二进制数 101 和 111 的每一位的或运算的结果值相同,如下所示。

The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.

  1. 1 & 1 = 1

  2. 1 & 0 = 0

  3. 1 & 1 = 1

所以,结果二进制数是 111,它等于十进制表示中的 7。

So, the resultant binary number is 111, which is equal to 7 in the decimal representation.

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  const b = 7;
  document.getElementById("output").innerHTML = "a & b = " + (a & b);
</script>
</body>
</html>

它将产生以下结果 −

It will produce the following result −

a & b = 5

JavaScript Bitwise OR (|) Operator

按位 OR (|) 运算符对其整数运算符的每一对位执行 OR 运算。运算后,它返回一个更新了位的整数。

The bitwise OR (|) operator performs OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.

当按位 OR 运算符应用于一对位时,如果任一位是 1,则返回 1,否则返回 0。

When bitwise OR operator is applied on a pair of bits, it returns 1 if either of bits is 1, otherwise returns 0.

以下是按位 OR 运算的真值表。

Following is the truth table for bitwise OR operation.

A

B

A

B

0

0

0

0

1

1

1

0

1

1

1

1

让我们以 4 位操作数为例来了解按位 OR 运算。

Let’s understand bitwise OR operation taking an example of 4-bit operands.

A

B

A

B

1111

0001

1111

1111

0010

1111

1111

0100

1111

1111

1000

1111

Example

我们对 5 和 7 执行按位 OR (|) 运算。这些数字表示为 32 位整数。

Let’s perform bitwise OR (|) operation on 5 and 7. These numbers are represented as 32-bits integer.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

7

00000000000000000000000000000111

5

7

二进制数 101 和 111 的每一位的或运算的结果值相同,如下所示。

The resultant value of the OR operation of each bit of the 101 and 111 binary numbers is the same as below.

  1. 1 | 1 = 1

  2. 1 | 0 = 1

  3. 1 | 1 = 1

所以,结果二进制数是 111,它等于十进制表示中的 7。

So, the resultant binary number is 111, which is equal to 7 in the decimal representation.

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  const b = 7;
  document.getElementById("output").innerHTML = "a | b = " + (a | b);
</script>
</body>
</html>

它将产生以下结果 −

It will produce the following result −

a | b = 7

JavaScript Bitwise XOR (^) Operator

按位异或 (^) 运算符对其整数操作数的每对位执行异或运算。运算后,它返回一个具有更新位的新整数。

The bitwise XOR (^) operator performs exclusive OR operation on each pair of bits of its integer operands. After the operation, it returns an integer value with the updated bits.

当对一对位应用按位异或运算符时,如果两个位不同则返回 1,否则返回 0。

When bitwise XOR operator is applied on a pair of bits, it returns 1 if both bits are different, otherwise returns 0.

以下是按位异或运算的真值表 -

Following is the truth table for Bitwise XOR operation −

A

B

A ^ B

0

0

0

0

1

1

1

0

1

1

1

0

Example

让我们对 5 和 7 执行按位异或 (^) 运算。

Let’s perform bitwise XOR (^) operation on 5 and 7.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

7

00000000000000000000000000000111

5 ^ 7

00000000000000000000000000000010 (= 2)

执行 101 和 111 的按位异或运算后,得到以下的结果二进制数。

After performing the bitwise XOR operation of 101 and 111, the resultant binary number is given below.

  1. 1 ^ 1 = 0

  2. 1 ^ 0 = 1

  3. 1 ^ 1 = 0

因此,结果二进制数是 010,等于 2。

So, the resultant binary number is 010, which is equal to 2.

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  const b = 7;
  document.getElementById("output").innerHTML = "a ^ b = " + (a ^ b);
</script>
</body>
</html>

它将生成如下输出:

It will produce the following output −

a ^ b = 2

JavaScript Bitwise NOT (~) Operator

按位非(~)运算符对二进制数的每一位执行非运算。它是一个一元运算符,它对二进制数的每一位取反,并返回二进制数的 2 的补码。

The bitwise NOT (~) operator performs the NOT operation on each bit of the binary number. It is a unary operator that inverts each bit of the binary number and returns the 2’s complement to the binary number.

以下是按位异或运算的真值表。

Following is the truth table for the Bitwise XOR operation.

Input (A)

Output (~A)

0

1

1

0

Example

让我们执行按位非(~)运算。

Let’s perform bitwise NOT (~) operation.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

7

00000000000000000000000000000111

~5

11111111111111111111111111111010 (= -6)

~7

11111111111111111111111111111000 (= -8)

尝试执行下面的代码 -

Try to execute the below code −

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  const b = 7;
  document.getElementById("output").innerHTML =
  "~a = " + (~a) + "<br>" +
  "~b = " + (~b)
</script>
</body>
</html>

它将生成如下输出:

It will produce the following output −

~a = -6
~b = -8

Bitwise Left Shift (<<) Operator

JavaScript 按位左移 (<<) 运算符将其第一个操作数中的所有位向左移动,位数由第二个操作数指定。用 0 填充新的位,丢弃最左位。

The JavaScript bitwise left shift (<<) operator moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros from the right and left most bits are discarded.

将值左移一位相当于将其乘以 2,左移两位相当于乘以 4,依此类推。

Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

Example

当你将 5(101)左移 1 位时,值变为 10(1010)。当你对 2 位执行左移操作时,结果值是 20(10100)。

When you left shift 5 (101) by 1, a value becomes 10 (1010). When you perform the left shift operation by 2 places, the resultant value is 20 (10100).

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

5 << 1

00000000000000000000000000001010 (= 10)

5 << 2

00000000000000000000000000010100 (= 20)

以下 JavaScript 程序演示位移左移操作 −

The following JavaScript program demonstrates the bitwise left shift operation −

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  document.getElementById("output").innerHTML =
  "a << 1 = " + (a << 1) + "<br>" +
  "a << 2 = " + (a << 2);
</script>
</body>
</html>

它将生成如下输出:

It will produce the following output −

a << 1 = 10
a << 2 = 20

Bitwise Right Shift (>>) Operator

位移右移 (>>) 运算符将第一个操作数中的所有位向右移动指定的第二个操作数位数。它从左插入最靠左的位副本并丢弃最靠右的位。以这种方式,它保留数字的符号。

The bitwise right shift (>>) operator moves all the bits in its first operand to the right by the number of places specified in the second operand. It inserts copies of leftmost bit in from left and discard rightmost bits. In this way it preserves the sign of the number.

简而言之,它从数字中删除 N 个最后一位。此处,N 是第二个操作数。右移二进制数相当于将十进制数除以 2。

In short, it removes the N last bits from the number. Here, N is a second operand. Right-shifting the binary number is equivalent to dividing the decimal number by 2.

Example

在下例中,当我们第一次对 101 执行右移操作时,a 的值变为 010。在执行右移操作第二次后,得到的结果值是 001,在十进制表示法中等于 1。

In the below example, when we perform the right shift operation on 101 for the first time, the value of a becomes equal to 010. After performing the right-shift operation for the second time, the resultant value is 001, equal to 1 in the decimal representation.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

5 >> 1

00000000000000000000000000000010 (= 2)

~5

11111111111111111111111111111010 (= -6)

~5 >>1

11111111111111111111111111111101 (= -3)

尝试执行以下程序 −

Try to execute the following program −

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  document.getElementById("output").innerHTML =
  "a >> 1 = " + (a >> 1) + "<br>" +
  "~a >> 1 = " + (~a >> 1);
</script>
</body>
</html>

它将生成如下输出:

It will produce the following output −

a >> 1 = 2
~a >> 1 = -3

Bitwise Right Shift with Zero (>>>) Operator

使用零的右移 (>>>) 运算符与右移运算符非常相似。它始终用零填充左位,而不必担心位的符号。

The Right Shift with Zero (>>>) operator is very similar to the right shift operator. It always fills the left bits with zero without worrying about the sign of the bit.

Example

此处,10 的二进制表示形式为 1010。当我们使用零执行右移操作时,它向右移动所有位 2 次,并在开头插入两个 0。因此,得到的结果值将是 0010,等于 1。

Here, the binary representation of 10 is 1010. When we perform the right shift with zero operation, it moves all bits 2 times in the right direction and inserts two 0 at the start. So, the resultant value will be 0010, equal to 1.

Decimal Number

Binary Equivalent (32-bits)

5

00000000000000000000000000000101

5 >>> 1

00000000000000000000000000000010 (= 2)

以下 JavaScript 程序演示如何使用 >>> 运算符。

The following JavaScript program demonstrate the use of >>> operator.

<html>
<body>
<div id="output"></div>
<script>
  const a = 5;
  document.getElementById("output").innerHTML = "a >>> 1 = " + (a >>> 1);
</script>
</body>
</html>

它将产生以下结果 −

It will produce the following result −

a >>> 1 = 2

您可能尝试对每个运算符使用不同的输入并观察输出以获取更多实践。

You may try to use the different inputs with each operator and observe the output for more practices.