Home  >  Article  >  Web Front-end  >  Javascript 按位与运算符 (&)使用介绍_基础知识

Javascript 按位与运算符 (&)使用介绍_基础知识

WBOY
WBOYOriginal
2016-05-16 17:01:29964browse

复制代码 代码如下:

result = 【整数1】 & 【整数1】

& 对两个 32 位表达式的每一个位执行按位“与”运算。 如果两个位均为 1,则结果是 1。 否则,结果为 0。

位1 位2 位与
0 0 0
1 1 1
0 1 0
1 0 0
下面的示例演示如何使用 & 位与运算符和 &= 按位与赋值运算符:

复制代码 代码如下:

// 9 二进制是 1001,补足32位为 00000000000000000000000000001001
var expr1 = 9;

// 5 是 00000000000000000000000000000101
var expr2 = 5;

/*
00000000000000000000000000001001
&
00000000000000000000000000000101
=
00000000000000000000000000000001
=
1
*/

var result = expr1 & expr2;

alert(result);
// 弹出【1】

expr1 &= expr2;
alert(expr1);
// 弹出【1】
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn