aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/left_shift/index.html
blob: 3c4f0b3b6a41b799dd3604d9fabc78c2deab5c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
title: 左移 (<<)
slug: Web/JavaScript/Reference/Operators/Left_shift
translation_of: Web/JavaScript/Reference/Operators/Left_shift
---
<div>{{jsSidebar("Operators")}}</div>

<p><strong>左移操作符 (<code>&lt;&lt;</code>)</strong> 将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-left-shift.html")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><code><var>a</var> &lt;&lt; <var>b</var></code>
</pre>

<h2 id="描述">描述</h2>

<p>左移操作符将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。</p>

<p>例如, <code>9 &lt;&lt; 2</code> 得出 36:</p>

<pre class="brush: js">.    9 (十进制): 00000000000000000000000000001001 (二进制)
                  --------------------------------
9 &lt;&lt; 2 (十进制): 00000000000000000000000000100100 (二进制) = 36 (十进制)
</pre>

<p>移动任意数字 <code>x</code> 至左边 <code>y</code> 位,得出 <code>x * 2 ** y</code> 。<br>
 所以例如:<code>9 &lt;&lt; 3</code> 等价于 <code>9 * 2³ = 9 * 8 = 72</code></p>

<h2 id="例子">例子</h2>

<h3 id="使用左移">使用左移</h3>

<pre class="brush: js">9 &lt;&lt; 3; // 72

// 9 * 2³ = 9 * 8 = 72
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-bitwise-shift-operators', 'Bitwise Shift Operators')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("javascript.operators.left_shift")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise">Bitwise operators in the JS guide</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift_assignment">Left shift assignment operator</a></li>
</ul>