# 비트 연산자

이 번역은 완료되지 않았습니다. 이 문서를 번역해 주세요.

비트 연산자는 피연산자를 10진수, 16진수, 8진수가 아니라, 32개의 비트(0과 1) 집합으로 취급합니다. 예를 들어, 10진수 9의 2진수 표기법은 1001입니다. 이렇게, 비트 연산자는 값의 2진수 표현을 사용해 연산하지만, 결과는 표준 JavaScript 숫자 값으로 반환합니다.

다음 표는 JavaScript의 비트 연산자에 대해 설명합니다.

연산자 사용방법 설명
비트 AND `a & b` 피연산자를 비트로 바꿨을 때 각각 대응하는 비트가 모두 1이면 그 비트값에 1을 반환.
비트 OR `a | b` 피연산자를 비트로 바꿨을 때 각각 대응하는 비트가 모두 1이거나 한 쪽이 1이면 1을 반환.
비트 XOR `a ^ b` 피연산자를 비트로 바꿨을 때 대응하는 비트가 서로 다르면 1을 반환.
비트 NOT `~ a` 피연산자의 반전된 값을 반환.
왼쪽 시프트 `a << b` Shifts `a` in binary representation `b` (< 32) bits to the left, shifting in zeros from the right.
부호 유지 오른쪽 시프트 `a >> b` Shifts `a` in binary representation `b` (< 32) bits to the right, discarding bits shifted off.
부호 버림 오른쪽 시프트 `a >>> b` Shifts `a` in binary representation `b` (< 32) bits to the right, discarding bits shifted off, and shifting in zeros from the left.

## 부호 있는 32비트 정수

The operands of all bitwise operators are converted to signed 32-bit integers in big-endian order and in two's complement format. Big-endian order means that the most significant bit (the bit position with the greatest value) is the left-most bit if the 32 bits are arranged in a horizontal line. Two's complement format means that a number's negative counterpart (e.g. 5 vs. -5) is all the number's bits inverted (bitwise NOT of the number, a.k.a. one's complement of the number) plus one. For example, the following encodes the integer 314 (base 10):

```00000000000000000000000100111010
```

The following encodes `~314`, i.e. the one's complement of `314`:

```11111111111111111111111011000101
```

Finally, the following encodes `-314,` i.e. the two's complement of `314`:

```11111111111111111111111011000110
```

The two's complement guarantees that the left-most bit is 0 when the number is positive and 1 when the number is negative. Thus, it is called the sign bit.

The number `0` is the integer that is composed completely of 0 bits.

```0 (base 10) = 00000000000000000000000000000000 (base 2)
```

The number `-1` is the integer that is composed completely of 1 bits.

```-1 (base 10) = 11111111111111111111111111111111 (base 2)
```

The number `-2147483648` (hexadecimal representation: `-0x80000000`) is the integer that is composed completely of 0 bits except the first (left-most) one.

```-2147483648 (base 10) = 10000000000000000000000000000000 (base 2)
```

The number `2147483647` (hexadecimal representation: `0x7fffffff`) is the integer that is composed completely of 1 bits except the first (left-most) one.

```2147483647 (base 10) = 01111111111111111111111111111111 (base 2)
```

The numbers `-2147483648` and `2147483647` are the minimum and the maximum integers representable throught a 32bit signed number.

## 비트 논리 연산자

비트 논리 연산자는 다음과 같이 사용됩니다.

• 피연산자는 32비트 정수로 변환되고, 이진법으로 표현됩니다 (0과 1).
• 이진법으로 표현된 첫 번째 피연산자는 두 번째 피연산자와 쌍을 이룹니다: 첫 번째는 첫 번째 비트끼리, 두 번째는 두 번째 비트끼리...
• 연산자는 각각의 비트쌍에 적용되고, 결과 또한 이진법으로 구성됩니다.

### & (비트 AND)

비트 연산자 AND를 비트 쌍으로 실행합니다.

Performs the AND operation on each pair of bits. `a` AND `b` yields 1 only if both `a` and `b` are 1. The truth table for the AND operation is:

 a b a AND b 0 0 0 0 1 0 1 0 0 1 1 1
```     9 (base 10) = 00000000000000000000000000001001 (base 2)
14 (base 10) = 00000000000000000000000000001110 (base 2)
--------------------------------
14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)
```

Bitwise ANDing any number `x` with `0` yields `0`. Bitwise ANDing any number `x` with `-1` yields `x`.

### | (비트 OR)

Performs the OR operation on each pair of bits. `a` OR `b` yields 1 if either `a` or `b` is 1. The truth table for the OR operation is:

 a b a OR b 0 0 0 0 1 1 1 0 1 1 1 1
```     9 (base 10) = 00000000000000000000000000001001 (base 2)
14 (base 10) = 00000000000000000000000000001110 (base 2)
--------------------------------
14 | 9 (base 10) = 00000000000000000000000000001111 (base 2) = 15 (base 10)
```

Bitwise ORing any number x with 0 yields x.

Bitwise ORing any number x with -1 yields -1.

### ^ (비트 XOR)

Performs the XOR operation on each pair of bits. `a` XOR `b` yields 1 if `a` and `b` are different. The truth table for the XOR operation is:

 a b a XOR b 0 0 0 0 1 1 1 0 1 1 1 0
```     9 (base 10) = 00000000000000000000000000001001 (base 2)
14 (base 10) = 00000000000000000000000000001110 (base 2)
--------------------------------
14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)
```

Bitwise XORing any number x with 0 yields x.

Bitwise XORing any number x with -1 yields ~x.

### ~ (비트 NOT)

Performs the NOT operator on each bit. NOT `a` yields the inverted value (a.k.a. one's complement) of `a`. The truth table for the NOT operation is:

 a NOT a 0 1 1 0
``` 9 (base 10) = 00000000000000000000000000001001 (base 2)
--------------------------------
~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
```

Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6.

## 비트 시프트 연산자

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.

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.

### << (Left shift)

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.

For example, `9 << 2` yields 36:

```     9 (base 10): 00000000000000000000000000001001 (base 2)
--------------------------------
9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)
```

Bitwise shifting any number x to the left by y bits yields x * 2^y.

### >> (Sign-propagating right shift)

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".

For example, `9 >> 2` yields 2:

```     9 (base 10): 00000000000000000000000000001001 (base 2)
--------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
```

Likewise, `-9 >> 2` yields -3, because the sign is preserved:

```     -9 (base 10): 11111111111111111111111111110111 (base 2)
--------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)
```

### >>> (Zero-fill right shift)

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.

For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, `9 >>> 2` yields 2, the same as `9 >> 2`:

```      9 (base 10): 00000000000000000000000000001001 (base 2)
--------------------------------
9 >>> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
```

However, this is not the case for negative numbers. For example, `-9 >>> 2` yields 1073741821, which is different than `-9 >> 2` (which yields -3):

```      -9 (base 10): 11111111111111111111111111110111 (base 2)
--------------------------------
-9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)
```

## 예제

### Flags and bitmasks

The bitwise logical operators are often used to create, manipulate, and read sequences of flags, 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).

Suppose there are 4 flags:

• flag A: we have an ant problem
• flag B: we own a bat
• flag C: we own a cat
• flag D: we own a duck

These flags are represented by a sequence of bits: DCBA. When a flag is set, it has a value of 1. When a flag is cleared, it has a value of 0. Suppose a variable `flags` has the binary value 0101:

```var flags = 5;   // binary 0101
```

This value indicates:

• flag A is true (we have an ant problem);
• flag B is false (we don't own a bat);
• flag C is true (we own a cat);
• flag D is false (we don't own a duck);

Since bitwise operators are 32-bit, 0101 is actually 00000000000000000000000000000101, but the preceding zeroes can be neglected since they contain no meaningful information.

A bitmask is a sequence of bits that can manipulate and/or read flags. Typically, a "primitive" bitmask for each flag is defined:

```var FLAG_A = 1; // 0001
var FLAG_B = 2; // 0010
var FLAG_C = 4; // 0100
var FLAG_D = 8; // 1000
```

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:

```var mask = FLAG_A | FLAG_B | FLAG_D; // 0001 | 0010 | 1000 => 1011
```

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 masks out the non-relevant flags by ANDing with zeros (hence the term "bitmask"). For example, the bitmask 0100 can be used to see if flag C is set:

```// if we own a cat
if (flags & FLAG_C) { // 0101 & 0100 => 0100 => true
// do stuff
}
```

A bitmask with multiple set flags acts like an "either/or". For example, the following two are equivalent:

```// if we own a bat or we own a cat
if ((flags & FLAG_B) || (flags & FLAG_C)) { // (0101 & 0010) || (0101 & 0100) => 0000 || 0100 => true
// do stuff
}
```
```// if we own a bat or cat
var mask = FLAG_B | FLAG_C; // 0010 | 0100 => 0110
if (flags & mask) { // 0101 & 0110 => 0100 => true
// do stuff
}
```

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:

```// yes, we own a cat and a duck
var mask = FLAG_C | FLAG_D; // 0100 | 1000 => 1100
flags |= mask;   // 0101 | 1100 => 1101
```

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:

```// no, we don't have an ant problem or own a cat
var mask = ~(FLAG_A | FLAG_C); // ~0101 => 1010
flags &= mask;   // 1101 & 1010 => 1000
```

The mask could also have been created with `~FLAG_A & ~FLAG_C` (De Morgan's law):

```// no, we don't have an ant problem, and we don't own a cat
var mask = ~FLAG_A & ~FLAG_C;
flags &= mask;   // 1101 & 1010 => 1000
```

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:

```// 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 => 1010
```

Finally, the flags can all be flipped with the NOT operator:

```// entering parallel universe...
flags = ~flags;    // ~1010 => 0101
```

### Conversion snippets

Convert a binary `string` to a decimal `number`:

```var sBinString = "1011";
var nMyNumber = parseInt(sBinString, 2);
alert(nMyNumber); // prints 11, i.e. 1011
```

Convert a decimal `number` to a binary `string`:

```var nMyNumber = 11;
var sBinString = nMyNumber.toString(2);
alert(sBinString); // prints 1011, i.e. 11
```

### Automate Mask Creation

If you have to create many masks from some `boolean` values, you can automatize the process:

```function createMask () {
var nMask = 0, nFlag = 0, nLen = arguments.length > 32 ? 32 : arguments.length;
for (nFlag; nFlag < nLen; nMask |= arguments[nFlag] << nFlag++);
}
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.

```

### Reverse algorithm: an array of booleans from a mask

If you want to create an `array` of `booleans` from a mask you can use this code:

```function arrayFromMask (nMask) {
// nMask must be between -2147483648 and 2147483647
if (nMask > 0x7fffffff || nMask < -0x80000000) { throw new TypeError("arrayFromMask - out of range"); }
for (var nShifted = nMask, aFromMask = []; nShifted; aFromMask.push(Boolean(nShifted & 1)), nShifted >>>= 1);
}

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
```

You can test both algorithms at the same time…

```var nTest = 19; // our custom mask

```

For didactic purpose only (since there is the `Number.toString(2)` method), we show how it is possible to modify the `arrayFromMask` algorithm in order to create a `string` containing the binary representation of a `number`, rather than an `array` of `booleans`:

```function createBinaryString (nMask) {
// nMask must be between -2147483648 and 2147483647
for (var nFlag = 0, nShifted = nMask, sMask = ""; nFlag < 32; nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
}

var string1 = createBinaryString(11);
var string2 = createBinaryString(4);
var string3 = createBinaryString(1);

// prints 00000000000000000000000000001011, i.e. 11
```

## 명세

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262) Standard Defined in several sections of the specification: Bitwise NOT operator, Bitwise shift operators, Binary bitwise operators
ECMAScript 2015 (6th Edition, ECMA-262) Standard Defined in several sections of the specification: Bitwise NOT operator, Bitwise shift operators, Binary bitwise operators
ECMAScript Latest Draft (ECMA-262) Draft Defined in several sections of the specification: Bitwise NOT operator, Bitwise shift operators, Binary bitwise operators

## 브라우저 호환성

Update compatibility data on GitHub
Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes Chrome Full support Yes Edge Full support Yes Firefox Full support 1 IE Full support Yes Opera Full support Yes Safari Full support Yes WebView Android Full support Yes Chrome Android Full support Yes Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes

Full support
Full support

## 문서 태그 및 공헌자

태그:
이 페이지의 공헌자: alattalatta, mdnwebdocs-bot, jghg2724, webix, daewon, teoli, Cho.Eun
최종 변경자: alattalatta,