blob: f049a499a9c1599cd95ea13182bdca257c4cd5ab (
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
67
68
69
70
71
72
73
74
75
|
---
title: Right shift (>>)
slug: Web/JavaScript/Reference/Operators/Right_shift
translation_of: Web/JavaScript/Reference/Operators/Right_shift
---
<div>{{jsSidebar("Operators")}}</div>
<p>The <strong>right shift operator (<code>>></code>)</strong> shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-right-shift.html")}}</div>
<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate"><code><var>a</var> >> <var>b</var></code>
</pre>
<h2 id="Description">Description</h2>
<p>This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".</p>
<p>For example, <code>9 >> 2</code> yields 2:</p>
<pre class="brush: js notranslate">. 9 (base 10): 00000000000000000000000000001001 (base 2)
--------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
</pre>
<p>Likewise, <code>-9 >> 2</code> yields <code>-3</code>, because the sign is preserved:</p>
<pre class="brush: js notranslate">. -9 (base 10): 11111111111111111111111111110111 (base 2)
--------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
</pre>
<h2 id="Examples">Examples</h2>
<h3 id="Using_right_shift">Using right shift</h3>
<pre class="brush: js notranslate"> 9 >> 2; // 2
-9 >> 2; // -3
</pre>
<h2 id="Specifications">Specifications</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="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.operators.right_shift")}}</p>
<h2 id="See_also">See also</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/Right_shift_assignment">Right shift assignment operator</a></li>
</ul>
<p>
<audio class="hidden"></audio>
</p>
|