JavaScript 按位运算符

JavaScript 按位运算符

按位运算符将操作数视为一组 32 位二进制数字(零和一)并执行操作。但是,结果显示为十进制值。

运算符 名称 示例
& 按位与 x & y
| 按位或 x | y
^ 按位异或 x ^ y
~ 按位非 ~x
<< 左移 x << y
>> 符号扩展右移 x >> y
>>> 零填充右移 x >>> y

注意:32 位有符号数可表示的最小和最大整数分别为 -2147483648 到 2147483647。


JavaScript 按位与

按位与 & 在两个操作数的相应位都是 1 时返回 1,否则返回 0

操作数 1 操作数 2 AND 运算
0 0 0 & 00
0 1 0 & 10
1 0 1 & 00
1 1 1 & 11

让我们来看一下整数 1225 的按位与运算。

In binary,

12 = 01100
25 = 11001

// Bitwise AND Operation of 12 and 25

    00001100
&   00011001
    ---------
    00001000  = 8 (In decimal)

注意:将 12 转换为 32 位二进制数为 00000000000000000000000000001100,将 25 转换为二进制数为 00000000000000000000000000011001。为简化起见,我们已省略前面的零。


示例 1:按位 AND 运算符

// bitwise AND operator example

let a = 12; 
let  b = 25; 

result = a & b; 
console.log(result); // 8 

在上面的程序中,

  • 12 的二进制值为 00000000000000000000000000001100
  • 25 的二进制值为 00000000000000000000000000011001
  • 当执行按位 AND 运算时,二进制结果为 00000000000000000000000000001000,其转换为十进制值为 8

JavaScript 按位或

按位或 | 在其中一个操作数的相应位为 1 时返回 1,否则返回 0

操作数 1 操作数 2 OR 运算
0 0 0 | 00
0 1 0 | 11
1 0 1 | 01
1 1 1 | 11

让我们来看一下整数 1225 的按位或运算。

In binary,

12 = 01100
25 = 11001

// Bitwise OR Operation of 12 and 25

    00001100
|   00011001
    --------
    00011101  = 29 (In decimal)

示例 2:按位 OR 运算符

// bitwise OR operator example
let a = 12; 
let  b = 25; 

result = a | b; 
console.log(result); // 29

当执行按位 OR 运算时,二进制结果为 00000000000000000000000000011101,其转换为十进制值为 29


JavaScript 按位异或

按位异或 ^ 在相应位不同时返回 1,在相应位相同时返回 0

操作数 1 操作数 2 XOR 运算
0 0 0 ^ 00
0 1 0 ^ 11
1 0 1 ^ 01
1 1 1 ^ 10
In binary,

12 = 01100
25 = 11001

// Bitwise XOR Operation of 12 and 25

    00001100
^   00011001
    --------
    00010101  = 21 (In decimal)

示例 3:按位 XOR 运算符

// bitwise XOR operator example
let a = 12; 
let  b = 25; 

result = a ^ b; 
console.log(result); // 21

当执行按位 XOR 运算时,二进制结果为 00000000000000000000000000010101,其转换为十进制值为 21


JavaScript 按位非

按位非 ~ 会反转位(0 变为 11 变为 0)。

In binary,

12 = 00000000000000000000000000001100

// Bitwise Not Operation of 12 

    ~ 00000000000000000000000000001100
      ---------------------------------
      11111111111111111111111111110011  = -13(In decimal)

11111111111111111111111111110011 转换为十进制时,值为 4294967283。但使用按位运算符时,该值是以带符号的 2 的补码格式计算的,零填充右移除外。

2 的补码是通过反转位(1 的补码)然后加 1 来计算的。例如,

13 in binary: 00000000000000000000000000001101
1's complement of 13: 11111111111111111111111111110010

2's complement of 13: 11111111111111111111111111110010
                                                    +1
                      ---------------------------------
                      11111111111111111111111111110011

注意 13 的 2 的补码(即 -13)是 11111111111111111111111111110011。这个值等于 12 的按位非。


示例 4:按位 NOT 运算符

// bitwise NOT operator example
let  b = 12;

result = ~b;

console.log(result); // -13

当执行按位 NOT 运算时,二进制结果为 11111111111111111111111111110011,其转换为十进制值为 -13

注意:数字 x 的按位非为 -(x + 1)。请注意,上面的 ~2 的结果是 -3


JavaScript 左移

在左移运算符 << 中,左操作数指定数字,右操作数指定要左移的位数。右侧会填充零位,左侧多余的位会被丢弃。

Working of left shift in JavaScript
JavaScript 中的一位左移

例如,

let a = 8;
let  b = 1;

result = a << b;

// 1 ( 00000000000000000000000000010000 )
console.log(result);

JavaScript 符号扩展右移

在右移运算符 >> 中,第一个操作数指定数字,第二个操作数指定要右移的位数。右侧多余的位会被丢弃。最左边的位的副本从左侧移入,因此称为符号扩展。

Working of sign propagating right fill in Javascript
JavaScript 中带符号扩展填充的一位右移

例如,

let a = 8;
let b = 1;
// 11111111111111111111111111111101
let c = -3;

result = a >> b;
result1 = c >> b;

// 4 (00000000000000000000000000000100)
console.log(result); 

// -2 (11111111111111111111111111111110)
console.log(result1); 

JavaScript 零填充右移

零填充右移 >>> 将操作数向右移动,并在左侧填充零位。右侧多余的位会被丢弃。

Working of right shift with zero fill in JavaScript
JavaScript 中带零填充的一位右移

例如,

let a = 8;
let b = 1;
let c = -3; 

result = a >>> b;
result1 = c >>> b;

// 4 (00000000000000000000000000000100)
console.log(result);

// 1073741823 (00111111111111111111111111111111)
console.log(result);
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战