aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html')
-rw-r--r--files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html557
1 files changed, 0 insertions, 557 deletions
diff --git a/files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html b/files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html
deleted file mode 100644
index e265714e18..0000000000
--- a/files/pt-br/conflicting/web/javascript/reference/operators_7c8eb9475d97a4a734c5991857698560/index.html
+++ /dev/null
@@ -1,557 +0,0 @@
----
-title: Bitwise operators
-slug: >-
- conflicting/Web/JavaScript/Reference/Operators_7c8eb9475d97a4a734c5991857698560
-translation_of: Web/JavaScript/Reference/Operators
-translation_of_original: Web/JavaScript/Reference/Operators/Bitwise_Operators
-original_slug: Web/JavaScript/Reference/Operators/Bitwise_Operators
----
-<div>{{jsSidebar("Operators")}}</div>
-
-<div></div>
-
-<div>Operadores bit-a-bit são são operadores tratados como sequência de 32 bits ( zeros e uns ), preferencialmente como decimal, hexadecimal, ou números octais. Por exemplo, o número decimal 9 tinha como representação binária de 1001. Operadores bit-a-bit realizam as operações em tais representações binárias, mas retornam valores numéricos no padrão Javascript.</div>
-
-<div>{{EmbedInteractiveExample("pages/js/expressions-bitwiseoperators.html")}}</div>
-
-<p class="hidden">O código para este interactivo exemplo está armazenado em um repositório GitHub . Se você quiser contribuir para o projeto exemplo interativo, por favor clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> e mande-nos um pull request.</p>
-
-<p>A seguinte tabela resume os Operadores bit-a-bit:</p>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <th>Operador</th>
- <th>Uso</th>
- <th>Descrição</th>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_AND)">Bitwise AND</a></td>
- <td><code>a &amp; b</code></td>
- <td>Retorna <code>1</code> em cada posição de bit para à qual o bit correspondente de ambos eram <code>1</code>s.</td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_OR)">Bitwise OR</a></td>
- <td><code>a | b</code></td>
- <td>
- <p>Retorna 1 para cada posição de bit para à qual o correspondente de um ou ambos eram <code>1</code>s.</p>
- </td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_XOR)">Bitwise XOR</a></td>
- <td><code>a ^ b</code></td>
- <td>Retorna 1 para cada posição de bit para à qual o bit correspondente de um mas não ambos eram <code>1</code>s.</td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_NOT)">Bitwise NOT</a></td>
- <td><code>~ a</code></td>
- <td>Inverte os bits de seus operandos.</td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#%3C%3C_(Left_shift)">Left shift</a></td>
- <td><code>a &lt;&lt; b</code></td>
- <td>Jogam <code>a</code>  em representação binária <code>b</code> (&lt; 32) bits à esquerda, mudando de zeros à diretia.</td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#%3E%3E_(Sign-propagating_right_shift)">Sign-propagating right shift</a></td>
- <td><code>a &gt;&gt; b</code></td>
- <td>Jogam <code>a</code>  em representação binária <code>b</code> (&lt; 32) bits à direita, descartando bits que foram tornados off.</td>
- </tr>
- <tr>
- <td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#%3E%3E%3E_(Zero-fill_right_shift)">Zero-fill right shift</a></td>
- <td><code>a &gt;&gt;&gt; b</code>  </td>
- <td>Jogam <code>a</code>  em representação binária <code>b</code> (&lt; 32) bits à direita, descartando bits que foram tornados off, e jogando <code>0</code>s para à esquerda.</td>
- </tr>
- </tbody>
-</table>
-
-<h2 id="Inteiros_assinados_em_32-bit">Inteiros assinados em 32-bit</h2>
-
-<p>Os operandos de todos os operadores bit-a-bit são assinados como inteiros de 32-bit em duas formas complementares. Duas formas complementares significa que uma negativa contrapartida (e.g. 5 vs. -5) são todos os bits daqueles números invertidos (bit-a-bit NOT de um número, a.k.a. complementos de um número) mais um. Por example, os seguintes encodes inteiros são 314:</p>
-
-<pre class="brush: js">00000000000000000000000100111010
-</pre>
-
-<p>Os seguintes encodes <code>~314</code>, i.e. são os únicos complementos de  <code>314</code>:</p>
-
-<pre class="brush: js">11111111111111111111111011000101
-</pre>
-
-<p>Finalmente, os seguintes encodes <code>-314,</code> i.e. são dois complementos de <code>314</code>:</p>
-
-<pre class="brush: js">11111111111111111111111011000110
-</pre>
-
-<p>As duas garantias complementares daquele bit mais à esquerda que é zero quando o número é positivo e 1 quando o número é negativo. Aliás, isto é chamado de <em>sign bit </em>ou <em>bit assinalado.</em></p>
-
-<p>O número 0 é o inteiro composto completamente de 0 bits.</p>
-
-<pre class="brush: js">0 (base 10) = 00000000000000000000000000000000 (base 2)
-</pre>
-
-<p>O número <code>-1</code> é o inteiro que é composto completamente de 1 bits.</p>
-
-<pre class="brush: js">-1 (base 10) = 11111111111111111111111111111111 (base 2)
-</pre>
-
-<p>O número <code>-2147483648</code> (representação hexadecimal: <code>-0x80000000</code>) é o inteiro completamente composto de 0 bits exceto o primeiro (left-most) único.</p>
-
-<pre class="brush: js">-2147483648 (base 10) = 10000000000000000000000000000000 (base 2)
-</pre>
-
-<p>O número <code>2147483647</code> (representação hexadecimal: <code>0x7fffffff</code>) é o inteiro composto completamente por bits 1, exceto pelo primeiro (o mais à esquerda).</p>
-
-<pre class="brush: js">2147483647 (base 10) = 01111111111111111111111111111111 (base 2)
-</pre>
-
-<p>Os números <code>-2147483648</code> e <code>2147483647</code> são, respectivamente, o minimo e o máximo inteiro representáveis atráves de um número de 32 bits assinados.</p>
-
-<h2 id="Operadores_lógico_bit-abit">Operadores lógico bit-abit</h2>
-
-<p>Conceitualmente, os operadores lógicos bit-abit funcionam da seguinte forma:</p>
-
-<ul>
- <li>Os operandos são convertidos para inteiros de 32 bits e expressados em uma série de bits (zeros e ums). Números com mais de 32 bits têm seus bits mais significativos descartados. Por exemplo, o inteiro com mais de 32 bits a seguir será convertido para um inteiro de 32 bits:
- <pre class="brush: js">Before: 11100110111110100000000000000110000000000001
-After: 10100000000000000110000000000001</pre>
- </li>
- <li>Cada bit no primeiro operando é pareado com o bit correspondente no segundo operando: primeiro bit para o primeiro bit, segundo bit para o segundo bit e assim por diante.</li>
- <li>O operador é aplicado para cada par de bits e o resultado é construído bit a bit.</li>
-</ul>
-
-<h3 id="(Bitwise_AND)"><a name="Bitwise_AND">&amp; (Bitwise AND)</a></h3>
-
-<p>Performa a operação AND em cada par de bits. <code>a</code> AND <code>b</code> retorna 1, apenas quando <code>a</code> e <code>b</code> são <code>1</code>. A tabela verdade para a operação AND é:</p>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <td class="header">a</td>
- <td class="header">b</td>
- <td class="header">a AND b</td>
- </tr>
- <tr>
- <td>0</td>
- <td>0</td>
- <td>0</td>
- </tr>
- <tr>
- <td>0</td>
- <td>1</td>
- <td>0</td>
- </tr>
- <tr>
- <td>1</td>
- <td>0</td>
- <td>0</td>
- </tr>
- <tr>
- <td>1</td>
- <td>1</td>
- <td>1</td>
- </tr>
- </tbody>
-</table>
-
-<pre class="brush: js">. 9 (base 10) = 00000000000000000000000000001001 (base 2)
- 14 (base 10) = 00000000000000000000000000001110 (base 2)
- --------------------------------
-14 &amp; 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
-</pre>
-
-<p>Performar a operação AND bit-a-bit de qualquer número <code>x</code> com <code>0</code> retornará <code>0</code>. Performar a operação AND bit-a-bit de qualquer número <code>x</code> com <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">-1</span></font> retornará <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">x</span></font>.</p>
-
-<h3 id="(Bitwise_OR)"><a name="Bitwise_OR">| (Bitwise OR)</a></h3>
-
-<p>Performa a operação OR em cada par de bits. <code>a</code> OR <code>b</code> retorna 1 se pelo menos <code>a</code> ou pelo menos <code>b</code> é <code>1</code>. As tabela versão para a operação <code>OR</code> é:</p>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <td class="header">a</td>
- <td class="header">b</td>
- <td class="header">a OR b</td>
- </tr>
- <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>1</td>
- </tr>
- </tbody>
-</table>
-
-<pre class="brush: js">. 9 (base 10) = 00000000000000000000000000001001 (base 2)
- 14 (base 10) = 00000000000000000000000000001110 (base 2)
- --------------------------------
-14 | 9 (base 10) = 00000000000000000000000000001111 (base 2) = 15 (base 10)
-</pre>
-
-<p>Performar a operação <code>OR</code> de qulalquer número <code>x</code> com <code>0</code> retornará <code>0</code>. Performar a operação <code>OR</code> de qualquer número <code>X</code> com <code>-1</code> retornará <code>-1</code>.</p>
-
-<h3 id="(Bitwise_XOR)"><a name="Bitwise_XOR">^ (Bitwise XOR)</a></h3>
-
-<p>Performs the XOR operation on each pair of bits. <code>a</code> XOR <code>b</code> yields 1 if <code>a</code> and <code>b</code> are different. The truth table for the <code>XOR</code> operation is:</p>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <td class="header">a</td>
- <td class="header">b</td>
- <td class="header">a XOR b</td>
- </tr>
- <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>
-
-<pre class="brush: js">. 9 (base 10) = 00000000000000000000000000001001 (base 2)
- 14 (base 10) = 00000000000000000000000000001110 (base 2)
- --------------------------------
-14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)
-</pre>
-
-<p>Bitwise XORing any number <code>x</code> with <code>0</code> yields x. Bitwise XORing any number <code>x</code> with <code>-1</code> yields <code>~x</code>.</p>
-
-<h3 id="(Bitwise_NOT)"><a name="Bitwise_NOT">~ (Bitwise NOT)</a></h3>
-
-<p>Performs the NOT operator on each bit. NOT <code>a</code> yields the inverted value (a.k.a. one's complement) of <code>a</code>. The truth table for the <code>NOT</code> operation is:</p>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <td class="header">a</td>
- <td class="header">NOT a</td>
- </tr>
- <tr>
- <td>0</td>
- <td>1</td>
- </tr>
- <tr>
- <td>1</td>
- <td>0</td>
- </tr>
- </tbody>
-</table>
-
-<pre class="brush: js"> 9 (base 10) = 00000000000000000000000000001001 (base 2)
- --------------------------------
-~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
-</pre>
-
-<p>Bitwise NOTing any number <code>x</code> yields <code>-(x + 1)</code>. For example, <code>~-5</code> yields <code>4</code>.</p>
-
-<p>Note that due to using 32-bit representation for numbers both <code>~-1</code> and <code>~4294967295</code> (2<sup>32</sup>-1) results in <code>0</code>.</p>
-
-<h2 id="Operadores_de_deslocamento_bit_a_bit">Operadores de deslocamento bit a bit</h2>
-
-<p>The bitwise shift operators take two operands: the first is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used.</p>
-
-<p>Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if not only the low five bits will be used.</p>
-
-<h3 id="&lt;&lt;_(Left_shift)"><a name="Left_shift">&lt;&lt; (Left shift)</a></h3>
-
-<p>This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.</p>
-
-<p>For example, <code>9 &lt;&lt; 2</code> yields 36:</p>
-
-<pre class="brush: js">. 9 (base 10): 00000000000000000000000000001001 (base 2)
- --------------------------------
-9 &lt;&lt; 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)
-</pre>
-
-<p>Bitwise shifting any number <code>x</code> to the left by <code>y</code> bits yields <code>x * 2 ** y</code>.</p>
-
-<h3 id=">>_(Sign-propagating_right_shift)"><a name="Right_shift">&gt;&gt; (Sign-propagating right shift)</a></h3>
-
-<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 &gt;&gt; 2</code> yields 2:</p>
-
-<pre class="brush: js">. 9 (base 10): 00000000000000000000000000001001 (base 2)
- --------------------------------
-9 &gt;&gt; 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
-</pre>
-
-<p>Likewise, <code>-9 &gt;&gt; 2</code> yields <code>-3</code>, because the sign is preserved:</p>
-
-<pre class="brush: js">. -9 (base 10): 11111111111111111111111111110111 (base 2)
- --------------------------------
--9 &gt;&gt; 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
-</pre>
-
-<h3 id=">>>_(Zero-fill_right_shift)"><a name="Unsigned_right_shift">&gt;&gt;&gt; (Zero-fill right shift)</a></h3>
-
-<p>This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative.</p>
-
-<p>For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, <code>9 &gt;&gt;&gt; 2</code> yields 2, the same as <code>9 &gt;&gt; 2</code>:</p>
-
-<pre class="brush: js">. 9 (base 10): 00000000000000000000000000001001 (base 2)
- --------------------------------
-9 &gt;&gt;&gt; 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
-</pre>
-
-<p>However, this is not the case for negative numbers. For example, <code>-9 &gt;&gt;&gt; 2</code> yields 1073741821, which is different than <code>-9 &gt;&gt; 2</code> (which yields <code>-3</code>):</p>
-
-<pre class="brush: js">. -9 (base 10): 11111111111111111111111111110111 (base 2)
- --------------------------------
--9 &gt;&gt;&gt; 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)
-</pre>
-
-<h2 id="Examples">Examples</h2>
-
-<h3 id="Flags_and_bitmasks">Flags and bitmasks</h3>
-
-<p>The bitwise logical operators are often used to create, manipulate, and read sequences of <em>flags</em>, which are like binary variables. Variables could be used instead of these sequences, but binary flags take much less memory (by a factor of 32).</p>
-
-<p>Suppose there are 4 flags:</p>
-
-<ul>
- <li>flag A: we have an ant problem</li>
- <li>flag B: we own a bat</li>
- <li>flag C: we own a cat</li>
- <li>flag D: we own a duck</li>
-</ul>
-
-<p>These flags are represented by a sequence of bits: DCBA. When a flag is <em>set</em>, it has a value of 1. When a flag is <em>cleared</em>, it has a value of 0. Suppose a variable <code>flags</code> has the binary value 0101:</p>
-
-<pre class="brush: js">var flags = 5; // binary 0101
-</pre>
-
-<p>This value indicates:</p>
-
-<ul>
- <li>flag A is true (we have an ant problem);</li>
- <li>flag B is false (we don't own a bat);</li>
- <li>flag C is true (we own a cat);</li>
- <li>flag D is false (we don't own a duck);</li>
-</ul>
-
-<p>Since bitwise operators are 32-bit, 0101 is actually 00000000000000000000000000000101, but the preceding zeroes can be neglected since they contain no meaningful information.</p>
-
-<p>A <em>bitmask</em> is a sequence of bits that can manipulate and/or read flags. Typically, a "primitive" bitmask for each flag is defined:</p>
-
-<pre class="brush: js">var FLAG_A = 1; // 0001
-var FLAG_B = 2; // 0010
-var FLAG_C = 4; // 0100
-var FLAG_D = 8; // 1000
-</pre>
-
-<p>New bitmasks can be created by using the bitwise logical operators on these primitive bitmasks. For example, the bitmask 1011 can be created by ORing FLAG_A, FLAG_B, and FLAG_D:</p>
-
-<pre class="brush: js">var mask = FLAG_A | FLAG_B | FLAG_D; // 0001 | 0010 | 1000 =&gt; 1011
-</pre>
-
-<p>Individual flag values can be extracted by ANDing them with a bitmask, where each bit with the value of one will "extract" the corresponding flag. The bitmask <em>masks</em> out the non-relevant flags by ANDing with zeroes (hence the term "bitmask"). For example, the bitmask 0100 can be used to see if flag C is set:</p>
-
-<pre class="brush: js">// if we own a cat
-if (flags &amp; FLAG_C) { // 0101 &amp; 0100 =&gt; 0100 =&gt; true
- // do stuff
-}
-</pre>
-
-<p>A bitmask with multiple set flags acts like an "either/or". For example, the following two are equivalent:</p>
-
-<pre class="brush: js">// if we own a bat or we own a cat
-// (0101 &amp; 0010) || (0101 &amp; 0100) =&gt; 0000 || 0100 =&gt; true
-if ((flags &amp; FLAG_B) || (flags &amp; FLAG_C)) {
- // do stuff
-}
-</pre>
-
-<pre class="brush: js">// if we own a bat or cat
-var mask = FLAG_B | FLAG_C; // 0010 | 0100 =&gt; 0110
-if (flags &amp; mask) { // 0101 &amp; 0110 =&gt; 0100 =&gt; true
- // do stuff
-}
-</pre>
-
-<p>Flags can be set by ORing them with a bitmask, where each bit with the value one will set the corresponding flag, if that flag isn't already set. For example, the bitmask 1100 can be used to set flags C and D:</p>
-
-<pre class="brush: js">// yes, we own a cat and a duck
-var mask = FLAG_C | FLAG_D; // 0100 | 1000 =&gt; 1100
-flags |= mask; // 0101 | 1100 =&gt; 1101
-</pre>
-
-<p>Flags can be cleared by ANDing them with a bitmask, where each bit with the value zero will clear the corresponding flag, if it isn't already cleared. This bitmask can be created by NOTing primitive bitmasks. For example, the bitmask 1010 can be used to clear flags A and C:</p>
-
-<pre class="brush: js">// no, we don't have an ant problem or own a cat
-var mask = ~(FLAG_A | FLAG_C); // ~0101 =&gt; 1010
-flags &amp;= mask; // 1101 &amp; 1010 =&gt; 1000
-</pre>
-
-<p>The mask could also have been created with <code>~FLAG_A &amp; ~FLAG_C</code> (De Morgan's law):</p>
-
-<pre class="brush: js">// no, we don't have an ant problem, and we don't own a cat
-var mask = ~FLAG_A &amp; ~FLAG_C;
-flags &amp;= mask; // 1101 &amp; 1010 =&gt; 1000
-</pre>
-
-<p>Flags can be toggled by XORing them with a bitmask, where each bit with the value one will toggle the corresponding flag. For example, the bitmask 0110 can be used to toggle flags B and C:</p>
-
-<pre class="brush: js">// if we didn't have a bat, we have one now,
-// and if we did have one, bye-bye bat
-// same thing for cats
-var mask = FLAG_B | FLAG_C;
-flags = flags ^ mask; // 1100 ^ 0110 =&gt; 1010
-</pre>
-
-<p>Finally, the flags can all be flipped with the NOT operator:</p>
-
-<pre class="brush: js">// entering parallel universe...
-flags = ~flags; // ~1010 =&gt; 0101
-</pre>
-
-<h3 id="Conversion_snippets">Conversion snippets</h3>
-
-<p>Convert a binary <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="/en-US/docs/JavaScript/Reference/Global_Objects/String">String</a></code> to a decimal <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number">Number</a></code>:</p>
-
-<pre class="brush: js">var sBinString = '1011';
-var nMyNumber = parseInt(sBinString, 2);
-alert(nMyNumber); // prints 11, i.e. 1011
-</pre>
-
-<p>Convert a decimal <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number">Number</a></code> to a binary <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="/en-US/docs/JavaScript/Reference/Global_Objects/String">String</a></code>:</p>
-
-<pre class="brush: js">var nMyNumber = 11;
-var sBinString = nMyNumber.toString(2);
-alert(sBinString); // prints 1011, i.e. 11
-</pre>
-
-<h3 id="Automate_Mask_Creation">Automate Mask Creation</h3>
-
-<p>You can create multiple masks from a set of <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="/en-US/docs/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code> values, like this:</p>
-
-<pre class="brush: js">function createMask() {
- var nMask = 0, nFlag = 0, nLen = arguments.length &gt; 32 ? 32 : arguments.length;
- for (nFlag; nFlag &lt; nLen; nMask |= arguments[nFlag] &lt;&lt; nFlag++);
- return nMask;
-}
-var mask1 = createMask(true, true, false, true); // 11, i.e.: 1011
-var mask2 = createMask(false, false, true); // 4, i.e.: 0100
-var mask3 = createMask(true); // 1, i.e.: 0001
-// etc.
-
-alert(mask1); // prints 11, i.e.: 1011
-</pre>
-
-<h3 id="Reverse_algorithm_an_array_of_booleans_from_a_mask">Reverse algorithm: an array of booleans from a mask</h3>
-
-<p>If you want to create an <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="/en-US/docs/JavaScript/Reference/Global_Objects/Array">Array</a></code> of <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="/en-US/docs/JavaScript/Reference/Global_Objects/Boolean">Booleans</a></code> from a mask you can use this code:</p>
-
-<pre class="brush: js">function arrayFromMask(nMask) {
- // nMask must be between -2147483648 and 2147483647
- if (nMask &gt; 0x7fffffff || nMask &lt; -0x80000000) {
- throw new TypeError('arrayFromMask - out of range');
- }
- for (var nShifted = nMask, aFromMask = []; nShifted;
- aFromMask.push(Boolean(nShifted &amp; 1)), nShifted &gt;&gt;&gt;= 1);
- return aFromMask;
-}
-
-var array1 = arrayFromMask(11);
-var array2 = arrayFromMask(4);
-var array3 = arrayFromMask(1);
-
-alert('[' + array1.join(', ') + ']');
-// prints "[true, true, false, true]", i.e.: 11, i.e.: 1011
-</pre>
-
-<p>You can test both algorithms at the same time…</p>
-
-<pre class="brush: js">var nTest = 19; // our custom mask
-var nResult = createMask.apply(this, arrayFromMask(nTest));
-
-alert(nResult); // 19
-</pre>
-
-<p>For the didactic purpose only (since there is the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString">Number.toString(2)</a></code> method), we show how it is possible to modify the <code>arrayFromMask</code> algorithm in order to create a <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="/en-US/docs/JavaScript/Reference/Global_Objects/String">String</a></code> containing the binary representation of a <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number">Number</a></code>, rather than an <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="/en-US/docs/JavaScript/Reference/Global_Objects/Array">Array</a></code> of <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="/en-US/docs/JavaScript/Reference/Global_Objects/Boolean">Booleans</a></code>:</p>
-
-<pre class="brush: js">function createBinaryString(nMask) {
- // nMask must be between -2147483648 and 2147483647
- for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag &lt; 32;
- nFlag++, sMask += String(nShifted &gt;&gt;&gt; 31), nShifted &lt;&lt;= 1);
- return sMask;
-}
-
-var string1 = createBinaryString(11);
-var string2 = createBinaryString(4);
-var string3 = createBinaryString(1);
-
-alert(string1);
-// prints 00000000000000000000000000001011, i.e. 11
-</pre>
-
-<h2 id="Specifications">Specifications</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('ES1')}}</td>
- <td>{{Spec2('ES1')}}</td>
- <td>Initial definition.</td>
- </tr>
- <tr>
- <td>{{SpecName('ES5.1', '#sec-11.7')}}</td>
- <td>{{Spec2('ES5.1')}}</td>
- <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.8">Bitwise NOT operator</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.7">Bitwise shift operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.10">Binary bitwise operators</a></td>
- </tr>
- <tr>
- <td>{{SpecName('ES6', '#sec-bitwise-shift-operators')}}</td>
- <td>{{Spec2('ES6')}}</td>
- <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-bitwise-not-operator">Bitwise NOT operator</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-bitwise-shift-operators">Bitwise shift operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-binary-bitwise-operators">Binary bitwise operators</a></td>
- </tr>
- <tr>
- <td>{{SpecName('ESDraft', '#sec-bitwise-shift-operators')}}</td>
- <td>{{Spec2('ESDraft')}}</td>
- <td>Defined in several sections of the specification: <a href="http://tc39.github.io/ecma262/#sec-bitwise-not-operator">Bitwise NOT operator</a>, <a href="http://tc39.github.io/ecma262/#sec-bitwise-shift-operators">Bitwise shift operators</a>, <a href="http://tc39.github.io/ecma262/#sec-binary-bitwise-operators">Binary bitwise operators</a></td>
- </tr>
- </tbody>
-</table>
-
-<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>
-
-<p>{{Compat("javascript.operators.bitwise")}}</p>
-
-<h2 id="See_also">See also</h2>
-
-<ul>
- <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators">Logical operators</a></li>
-</ul>