読者です 読者をやめる 読者になる 読者になる

ビットシフト

とくに意味はなく視覚的に安心したいのでメモ

"x >> 1" は x を 2^1 で割った商
0 >> 1 = 0
1 >> 1 = 0
2 >> 1 = 1
3 >> 1 = 1
4 >> 1 = 2
5 >> 1 = 2
6 >> 1 = 3
7 >> 1 = 3
8 >> 1 = 4
9 >> 1 = 4
10 >> 1 = 5
11 >> 1 = 5
12 >> 1 = 6
13 >> 1 = 6
14 >> 1 = 7
15 >> 1 = 7

"x >> 2" は x を 2^2 で割った商
0 >> 2 = 0
1 >> 2 = 0
2 >> 2 = 0
3 >> 2 = 0
4 >> 2 = 1
5 >> 2 = 1
6 >> 2 = 1
7 >> 2 = 1
8 >> 2 = 2
9 >> 2 = 2
10 >> 2 = 2
11 >> 2 = 2
12 >> 2 = 3
13 >> 2 = 3
14 >> 2 = 3
15 >> 2 = 3

以下同様に "x >> n" は x を 2^n で割った商

"x << 1" は x の 2^1 倍
0 << 1 = 0
1 << 1 = 2
2 << 1 = 4
3 << 1 = 6
4 << 1 = 8
5 << 1 = 10
6 << 1 = 12
7 << 1 = 14
8 << 1 = 16
9 << 1 = 18
10 << 1 = 20
11 << 1 = 22
12 << 1 = 24
13 << 1 = 26
14 << 1 = 28
15 << 1 = 30

"x << 2" は x の 2^2 倍
0 << 2 = 0
1 << 2 = 4
2 << 2 = 8
3 << 2 = 12
4 << 2 = 16
5 << 2 = 20
6 << 2 = 24
7 << 2 = 28
8 << 2 = 32
9 << 2 = 36
10 << 2 = 40
11 << 2 = 44
12 << 2 = 48
13 << 2 = 52
14 << 2 = 56
15 << 2 = 60

以下同様に "x << n" は x の 2^n 倍