2019-04-21 11:50:48 +08:00

112 lines
4.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>Math.clz32() </strong></code>函数返回一个数字在转换成 32 无符号整形数字的二进制形式后, 开头的 0 的个数, 比如 <code>1000000</code> 转换成 32 位无符号整形数字的二进制形式后是 <code>00000000000011110100001001000000</code>, 开头的 0 的个数是 12 个, 则 <code>Math.clz32(1000000)</code> 返回 <code>12</code>.</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code>Math.clz32 (x)
</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt>x</dt>
<dd>一个数字.</dd>
</dl>
<h2 id="描述" style="line-height: 30px;">描述</h2>
<p>"clz32" 是 CountLeadingZeroes32 的缩写.</p>
<p>如果 <code>x</code> 不是数字类型, 则它首先会被转换成数字类型, 然后再转成 32 位无符号整形数字. </p>
<p>如果转换后的 32 位无符号整形数字是 <code>0</code>, 则返回 <code>32</code>, 因为此时所有位上都是 <code>0</code>.</p>
<p><code>NaN</code>, <code>Infinity</code>,<code> -Infinity</code> 这三个数字转成 32 位无符号整形数字后都是 <code>0</code>.</p>
<p><span>这个函数主要用于那些编译目标为 JS 语言的系统中, 比如 Emscripten.</span></p>
<h2 id="示例">示例</h2>
<pre><code class="language-javascript">Math.clz32(1) // 31
Math.clz32(1000) // 22
Math.clz32() // 32
[NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter(function (n) {
return Math.clz32(n) !== 32
}) // []
Math.clz32(true) // 31
Math.clz32(3.5) // 30
</code></pre>
<h2 id="Compatibility" name="Compatibility">Polyfill</h2>
<pre><code class="language-javascript">Math.clz32 = Math.clz32 || function(value) {
var value = Number(value) &gt;&gt;&gt; 0;
return value ? 32 - value.toString(2).length : 32;
}
</code></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><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-Math.clz32" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Math.clz32</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>35</td>
<td><a href="/en-US/Firefox/Releases/31" title="Released on 2014-07-22.">31</a> (31)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>31.0 (31)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Math" title="Math 是一个内置对象 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a></li>
</ul>
</article>