aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/math/clz32/index.html
blob: 74c1cecef7b15e75804874de9adcde36309cc338 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: Math.clz32()
slug: Web/JavaScript/Reference/Global_Objects/Math/clz32
translation_of: Web/JavaScript/Reference/Global_Objects/Math/clz32
original_slug: Web/JavaScript/Referencia/Objectes_globals/Math/clz32
---
<div>{{JSRef}}</div>

<p>La funció <strong><code>Math.clz32()</code></strong> retorna el nombre de zeros a l'esquerra que apareixen en una representació binària de 32 bits per a un nombre.</p>

<h2 id="Sintaxi">Sintaxi</h2>

<pre class="syntaxbox"><code>Math.clz32(<var>x</var>)</code></pre>

<h3 id="Paràmetres">Paràmetres</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>Un nombre.</dd>
</dl>

<h2 id="Descripció">Descripció</h2>

<p>"<code>clz32</code>" és una abreviació de <code>CountLeadingZeroes32</code>.</p>

<p>Si <code>x</code> no és un nombre, primer es convertirà a un nombre, i després es convertirà a un nombre sencer de 32 bits sense signe.</p>

<p>Si el nombre sencer sense signe de 32 bits és <code>0</code>, la funció retornarà <code>32</code> ja que tots els bits són <code>0</code>.</p>

<p>Aquesta funció és particulament útil per a sistemes que compilin en JavaScript, com ara <a href="/en-US/docs/Emscripten">Emscripten</a>.</p>

<h2 id="Exemples">Exemples</h2>

<h3 id="Utilitzar_Math.clz32()">Utilitzar <code>Math.clz32()</code></h3>

<pre class="brush: js">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
</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>Aquesta funció polyfill utilitza {{jsxref("Math.imul")}}.</p>

<pre class="brush: js">Math.clz32 = Math.clz32 || (function () {
  'use strict';

  var table = [
    32, 31,  0, 16,  0, 30,  3,  0, 15,  0,  0,  0, 29, 10,  2,  0,
     0,  0, 12, 14, 21,  0, 19,  0,  0, 28,  0, 25,  0,  9,  1,  0,
    17,  0,  4,   ,  0,  0, 11,  0, 13, 22, 20,  0, 26,  0,  0, 18,
     5,  0,  0, 23,  0, 27,  0,  6,  0, 24,  7,  0,  8,  0,  0,  0]

  // Adaptat d'un algorisme trobat a Hacker's Delight, pàgina 103.
  return function (x) {
    // Tingueu en compte que les variables no tenen perquè ser les mateixes.

    // 1. On n = ToUint32(x).
    var v = Number(x) &gt;&gt;&gt; 0

    // 2. On p és el nombre de zeros a l'esquerra en la representació binària de 32 bits de n.
    v |= v &gt;&gt;&gt; 1
    v |= v &gt;&gt;&gt; 2
    v |= v &gt;&gt;&gt; 4
    v |= v &gt;&gt;&gt; 8
    v |= v &gt;&gt;&gt; 16
    v = table[Math.imul(v, 0x06EB14F9) &gt;&gt;&gt; 26]

    // Retorna p.
    return v
  }
})();
</pre>

<h2 id="Especificacions">Especificacions</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificació</th>
   <th scope="col">Estat</th>
   <th scope="col">Comentaris</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.clz32', 'Math.clz32')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Definició inicial.</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Suport bàsic</td>
   <td>{{CompatChrome("38")}}</td>
   <td>{{CompatGeckoDesktop("31")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera("25")}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</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>Suport bàsic</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("31")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Vegeu_també">Vegeu també</h2>

<ul>
 <li>{{jsxref("Math")}}</li>
</ul>