diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/atomics/xor | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/atomics/xor')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html new file mode 100644 index 0000000000..83b0bdfb46 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html @@ -0,0 +1,126 @@ +--- +title: Atomics.xor() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/xor +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/xor +--- +<div>{{JSRef}}</div> + +<div><code><strong>Atomics</strong></code><strong><code>.xor()</code></strong> 静态方法会在数组中给定位置进行一次按位异或操作,并返回该位置的旧值。这个原子操作保证在修改后的值被写回之前不会发生其他写操作。</div> + +<div></div> + +<div>{{EmbedInteractiveExample("pages/js/atomics-xor.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Atomics.xor(typedArray, index, value) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>typedArray</code></dt> + <dd>一个共享的整型 typed array。例如 {{jsxref("Int8Array")}}, {{jsxref("Uint8Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int32Array")}}, 或者 {{jsxref("Uint32Array")}}.</dd> + <dt><code>index</code></dt> + <dd><code>typedArray</code> 中需要进行按位异或的索引位置。</dd> + <dt><code>value</code></dt> + <dd>要进行按位异或的数字。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>给定位置的旧值 (<code>typedArray[index]</code>)。</p> + +<h3 id="异常">异常</h3> + +<ul> + <li>假如 <code>typedArray</code> 不是允许的整型之一,则抛出 {{jsxref("TypeError")}}。</li> + <li>假如 <code>typedArray</code> 不是一个共享的整型 typed array,则抛出 {{jsxref("TypeError")}}。</li> + <li>如果 <code>index</code> 超出了 <code>typedArray</code> 的边界,则抛出 {{jsxref("RangeError")}}。</li> +</ul> + +<h2 id="描述">描述</h2> + +<p>如果a和b不同,则按位异或操作产生1。异或操作的真值表如下:</p> + +<table class="standard-table"> + <thead> + <tr> + <th><code>a</code></th> + <th><code>b</code></th> + <th><code>a ^ b</code></th> + </tr> + </thead> + <tbody> + <tr> + <td>0</td> + <td>0</td> + <td>0</td> + </tr> + <tr> + <td>0</td> + <td>1</td> + <td>1</td> + </tr> + <tr> + <td>1</td> + <td>0</td> + <td>1</td> + </tr> + <tr> + <td>1</td> + <td>1</td> + <td>0</td> + </tr> + </tbody> +</table> + +<p>例如,按位异或 <code>5 & 1</code> 将返回 <code>0100</code>,而 <code>0100</code> 是十进制为 <code>4</code> 。</p> + +<pre>5 0101 +1 0001 + ---- +4 0100 +</pre> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">const sab = new SharedArrayBuffer(1024); +const ta = new Uint8Array(sab); +ta[0] = 5; + +Atomics.xor(ta, 0, 1); // returns 5, the old value +Atomics.load(ta, 0); // 4</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-atomics.xor', 'Atomics.xor')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td>Initial definition in ES2017.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + + + +<p>{{Compat("javascript.builtins.Atomics.xor")}}</p> + +<h2 id="相关">相关</h2> + +<ul> + <li>{{jsxref("Atomics")}}</li> + <li>{{jsxref("Atomics.and()")}}</li> + <li>{{jsxref("Atomics.or()")}}</li> +</ul> |