# JavaScript按位运算符用法指南

2021年3月22日15:29:08 发表评论 580 次浏览

``````<script>
var a = 4;
var b = 1;

document.write( "A & B = " + (a & b) + '<br>' );

document.write( "A | B = " + (a | b) + '<br>' );

document.write( "~A = " + (~a) + '<br>' );
</script>``````

``````A & B = 0
A | B = 5
~A = -5``````

0 0 0
0 1 0
1 0 0
1 1 1

0 0 0
0 1 1
1 0 1
1 1 1

0 0 0
0 1 1
1 0 1
1 1 0

0 1
1 0

一种 6(00000000000000000000000000000110) 1(00000000000000000000000000000001) 12(00000000000000000000000000001100)

一种 6(00000000000000000000000000000110) 1(00000000000000000000000000000001) 3(00000000000000000000000000000011)

一种 6(00000000000000000000000000000110) 1(00000000000000000000000000000001) 3(00000000000000000000000000000011)

``````<script>
var a = 6;
var b = 1;

// AND Operation
document.write( "A & B = " + (a & b) + '<br>' );

// OR operation
document.write( "A | B = " + (a | b) + '<br>' );

// NOT operation
document.write( "~A = " + (~a) + '<br>' );

// Sign Propagating Right Shift
document.write( "A >> B = " + (a >> b) + '<br>' );

// Zero Fill Right Shift
document.write( "A >>> B = " + (a >>> b) + '<br>' );

// Left Shift
document.write( "A << B = " + (a << b) + '<br>' );
</script>``````

``````A & B = 0
A | B = 7
~A = -7
A >> B = 3
A >>> B = 3
A << B = 12``````

• 谷歌浏览器
• IE浏览器
• 火狐浏览器
• 苹果Safari
• 歌剧