-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Numeric Separatorsの対応、ビット演算子の書き直し #1312
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2e7883d
feat(basic): ビット演算子の書き直し
azu 28bc493
fix: update babel
azu 0185f1d
fix
azu 79d78ab
update image
azu 66513e8
CI: drop Node.js 10
azu caaac01
CI: remove env
azu 274000f
fix
azu a6d56d4
chore: update image
azu dec7130
feat(data-type): Numeric Separators
azu 80c14e6
fix
azu 2fd954d
add js
azu 1cfc11c
update
azu f6bd597
乗の表現にかえた
azu d874494
fix
azu 6fb5fb5
fix
azu 3690792
fix
azu b5eaf18
fix
azu 093f44f
feat(cheatsheet): Numeric Separatorsを追加
azu dca0202
fix
azu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -487,69 +487,104 @@ console.log(43 <= 42); // => false | |
|
||
## ビット演算子 {#bit-operator} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ビット演算は書き直し。 |
||
|
||
ビット演算子はオペランドを符号付き32bit整数に変換してから演算します。 | ||
ビット演算子による演算結果は10進数の数値を返します。 | ||
ビット演算子では、オペランドである数値を符号付き32ビット整数(`0`と`1`からなる32個のビットの集合)として扱います。 | ||
|
||
たとえば、`9`という数値は符号付き32bit整数では次のように表現されます。 | ||
たとえば、`1`という数値は符号付き32ビット整数のビットでは、`00000000000000000000000000000001` として表現されます。 | ||
わかりやすく4ビットごとに区切ると `0000_0000_0000_0000_0000_0000_0000_0001` のような32ビットの集合となります。 | ||
符号付き32ビット整数では、先頭の最上位ビット(一番左のビット)は符号を表し、`0`の場合は正の値、`1`の場合は負の値であることを示しています。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(0b0000000000000000000000000001001); // => 9 | ||
// Number#toStringメソッドを使うことで2進数表記の文字列を取得できる | ||
console.log((9).toString(2)); // => "1001" | ||
``` | ||
 | ||
|
||
また、`-9`という数値は、ビッグエンディアンの2の補数形式で表現されるため、次のようになります。 | ||
符号付き32ビット整数では負の数値は、2の補数形式という形式で表現されます。 | ||
2の補数とは、それぞれのビットを反転して1ビットを足した値となります。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(0b11111111111111111111111111110111); // => 4294967287 | ||
// ゼロ桁埋め右シフトをしてからNumber#toStringで2進数表記を取得できる | ||
console.log((-9 >>> 0).toString(2)); // => "11111111111111111111111111110111" | ||
``` | ||
たとえば、`-1` という数値の符号付き32ビット整数は、次のように2の補数で求められます。 | ||
|
||
- 10進数の`1`は、符号付き32ビット整数では`0000_0000_0000_0000_0000_0000_0000_0001`となる | ||
- `0000_0000_0000_0000_0000_0000_0000_0001` の各ビットを反転すると `1111_1111_1111_1111_1111_1111_1111_1110` となる | ||
- これに1ビットを足すと `1111_1111_1111_1111_1111_1111_1111_1111` となる | ||
|
||
これによって、`-1`の符号付き32ビット整数は `1111_1111_1111_1111_1111_1111_1111_1111` となります。 | ||
|
||
 | ||
|
||
<!-- textlint-disable ja-technical-writing/sentence-length --> | ||
|
||
そのため符号付き32ビット整数で表現できる数値の範囲は-2の31乗(`1111_1111_1111_1111_1111_1111_1111_1111`)から2の31乗(`0111_1111_1111_1111_1111_1111_1111_1111` )までとなります。 | ||
32ビットを超える数値については、32ビットをはみ出るビットが最上位(一番左)から順番に捨てられます。 | ||
|
||
<!-- textlint-enable ja-technical-writing/sentence-length --> | ||
これから見ていくビット演算子はオペランドを符号付き32ビット整数として扱い、その演算結果を10進数の数値として返します。 | ||
|
||
### ビット論理積(`&`) {#bit-and} | ||
|
||
論理積演算子(`&`)はビットごとの**AND**演算した結果を返します。 | ||
ビット論理積演算子(`&`)はビットごとの**AND**演算した結果を返します。 | ||
AND演算では、オペランドの各ビットがどちらも`1`の場合は`1`となり、それ以外の場合は`0`となります。 | ||
|
||
次のコードでは、10進数の`15`と`9`をAND演算しています。 | ||
`15`は、符号付き32ビット整数では`0000_0000_0000_0000_0000_0000_0000_1111`となります。 | ||
`9`は、符号付き32ビット整数では`0000_0000_0000_0000_0000_0000_0000_1001`となります。 | ||
これらをAND演算した結果は`0000_0000_0000_0000_0000_0000_0000_1001`となり、10進数の値である`9`を返します。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(15 & 9); // => 9 | ||
// 同じ位の各ビット同士をAND演算する(上位の`0`は省略) | ||
// 1111 | ||
// 1001 | ||
// ---- | ||
// 1001 | ||
console.log(0b1111 & 0b1001); // => 0b1001 | ||
``` | ||
|
||
### ビット論理和(`|`) {#bit-or} | ||
|
||
論理和演算子(`|`)はビットごとの**OR**演算した結果を返します。 | ||
ビット論理和演算子(`|`)はビットごとの**OR**演算した結果を返します。 | ||
OR演算では、オペランドの各ビットがどちらか片方でも`1`の場合は`1`となり、両方とも`0`の場合は`0`となります。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(15 | 9); // => 15 | ||
// 同じ位の各ビット同士をOR演算する(上位の`0`は省略) | ||
// 1111 | ||
// 1001 | ||
// ---- | ||
// 1111 | ||
console.log(0b1111 | 0b1001); // => 0b1111 | ||
``` | ||
|
||
### ビット排他的論理和(`^`) {#bit-xor} | ||
|
||
排他的論理和演算子(`^`)はビットごとの**XOR**演算した結果を返します。 | ||
ビット排他的論理和演算子(`^`)はビットごとの**XOR**演算した結果を返します。 | ||
XOR演算では、オペランドのビットが異なるなら`1`、両方とも同じなら`0`となります。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(15 ^ 9); // => 6 | ||
// 同じ位の各ビット同士をXOR演算する(上位の`0`は省略) | ||
// 1111 | ||
// 1001 | ||
// ---- | ||
// 0110 | ||
console.log(0b1111 ^ 0b1001); // => 0b0110 | ||
``` | ||
|
||
### ビット否定(`~`) {#bit-not} | ||
|
||
単項演算子の否定演算子(`~`)はオペランドを反転した値を返します。 | ||
単項演算子の否定演算子(`~`)はオペランドの各ビットを反転した値を返します。 | ||
これは1の補数として知られている値と同じものです。 | ||
|
||
次のコードでは、10進数で`15`を否定演算子(`~`)で各ビットを反転させた値を得ています。 | ||
`15` は `0000_0000_0000_0000_0000_0000_0000_1111`です。 | ||
各ビットを反転させると`1111_1111_1111_1111_1111_1111_1111_0000` となり、10進数では`-16` となります。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log(~15); // => -16 | ||
console.log(~0b1111); // => -0b10000 | ||
``` | ||
|
||
否定演算子(`~`)はビット演算以外でも使われていることがあります。 | ||
`~x`のように`x`をビット否定演算子で演算した結果は、`-(x + 1)`となります。 | ||
この性質を利用する形で、ビット否定演算子(`~`)はビット演算以外でも使われていることがあります。 | ||
|
||
文字列(Stringオブジェクト)が持つ`indexOf`メソッドは、マッチする文字列を見つけて、そのインデックス(位置)を返すメソッドです。 | ||
この`indexOf`メソッドは、検索対象が見つからない場合には`-1`を返します。 | ||
|
@@ -574,7 +609,7 @@ console.log(~(-1)); // => 0 | |
|
||
JavaScriptでは`0`も、if文では`false`として扱われます。 | ||
そのため、`~indexOfの結果`が`0`となるのは、その文字列が見つからなかった場合だけとなります。 | ||
次のコードのような否定演算子(`~`)と`indexOf`メソッドを使ったイディオムが一部では使われていました。 | ||
次のコードのように否定演算子(`~`)と`indexOf`メソッドを使ったイディオムが一部では使われていました。 | ||
|
||
{{book.console}} | ||
```js | ||
|
@@ -620,7 +655,7 @@ num << bit; | |
{{book.console}} | ||
```js | ||
console.log( 9 << 2); // => 36 | ||
console.log(0b1111 << 2); // => 0b111100 | ||
console.log(0b1111 << 2); // => 0b11_1100 | ||
``` | ||
|
||
### 右シフト演算子(`>>`) {#right-shift} | ||
|
@@ -639,20 +674,23 @@ num >> bit; | |
{{book.console}} | ||
```js | ||
console.log((-9) >> 2); // => -3 | ||
// 0b11111111111111111111111111110111 >> 2; // => 0b11111111111111111111111111111101 | ||
// 1111_1111_1111_1111_1111_1111_1111_0111 >> 2 | ||
// => 1111_1111_1111_1111_1111_1111_1111_1101 | ||
``` | ||
|
||
### ゼロ埋め右シフト演算子(`>>>`) {#fill-zero-right-shift} | ||
|
||
ゼロ埋め右シフト演算子は、数値である`num`を`bit`の数だけ右へシフトするのは右シフト演算子(`>>`)と同じです。異なる点としては右にあふれたビットは破棄され、`0`のビットを左から詰めます。 | ||
ゼロ埋め右シフト演算子は、数値である`num`を`bit`の数だけ右へシフトするのは右シフト演算子(`>>`)と同じです。 | ||
異なる点としては右にあふれたビットは破棄され、`0`のビットを左から詰めます。 | ||
|
||
次のコードでは、`-9`を2ビット分だけゼロ埋め右シフトしています。 | ||
左端のビットは`0`となるため、常に正の値となります。 | ||
|
||
{{book.console}} | ||
```js | ||
console.log((-9) >>> 2); // => 1073741821 | ||
// 0b11111111111111111111111111110111 >>> 2; // => 0b00111111111111111111111111111101 | ||
// 1111_1111_1111_1111_1111_1111_1111_0111 >>> 2 | ||
// => 0011_1111_1111_1111_1111_1111_1111_1101 | ||
``` | ||
|
||
<!-- textlint-enable eslint --> | ||
|
Binary file added
BIN
+50.8 KB
source/basic/operator/img/0000_0000_0000_0000_0000_0000_0000_0001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numeric Separatorsをデータ型に追加。
それぞれのリテラルに書くかまよったけど、Numeric Separatorsとして数値のリテラルで使えるよという形にした。