Bitshifting(位移):有人可以解释一下这段代码是做什么的吗?

huangapple go评论101阅读模式
英文:

Bitshifting: Can someone explain what this code does?

问题

所以,我正在阅读一本关于Go语言的书(《Go的道路》作者:Ivo Balbaert),其中有一个代码示例:

const hardEight = (1 << 100) >> 97

由于我在这台机器上没有安装Go,所以我决定将其翻译为PHP以查看结果(通过http://writecodeonline.com/php/,因为我在这台机器上也没有安装PHP):

echo (1 << 100) >> 97;

上述代码的结果是8....嗯?所以我决定写一个从0到100的for循环来查看结果:

for($i = 0; $i <= 100; $i++){
    echo $i . ": ";
    echo ($i << 100) >> 97;
    echo "<br>";
}

然而,结果是:

0: 0
1: 8
2: 16
3: 24
4: 32
5: 40
6: 48
7: 56
8: 64
9: 72
10: 80
11: 88
12: 96
13: 104
14: 112
15: 120
16: 128
17: 136
18: 144
19: 152
20: 160
21: 168
22: 176
23: 184
24: 192
25: 200
26: 208
27: 216
28: 224
29: 232
30: 240
31: 248
32: 256
33: 264
34: 272
35: 280
36: 288
37: 296
38: 304
39: 312
40: 320
41: 328
42: 336
43: 344
44: 352
45: 360
46: 368
47: 376
48: 384
49: 392
50: 400
51: 408
52: 416
53: 424
54: 432
55: 440
56: 448
57: 456
58: 464
59: 472
60: 480
61: 488
62: 496
63: 504
64: 512
65: 520
66: 528
67: 536
68: 544
69: 552
70: 560
71: 568
72: 576
73: 584
74: 592
75: 600
76: 608
77: 616
78: 624
79: 632
80: 640
81: 648
82: 656
83: 664
84: 672
85: 680
86: 688
87: 696
88: 704
89: 712
90: 720
91: 728
92: 736
93: 744
94: 752
95: 760
96: 768
97: 776
98: 784
99: 792
100: 800

有人能解释一下这段代码发生了什么吗?我知道这是位移操作,但我不太理解位移操作的原理,无法确定其中发生了什么。谢谢 Bitshifting(位移):有人可以解释一下这段代码是做什么的吗?

英文:

So, I'm reading a book on Go (The Way to Go by Ivo Balbaert), and in it there is a code sample:

const hardEight = (1 &lt;&lt; 100) &gt;&gt; 97

Since I don't have Go installed on this machine, I decided to translate it to PHP to see the results (via http://writecodeonline.com/php/ since I don't have PHP installed on this machine either):

echo (1 &lt;&lt; 100) &gt;&gt; 97;

The result for the above is 8....huh? So I wrote decided ok, lets write a for-loop from 0 to 100 and see the results:

for($i = 0; $i &lt;= 100; $i++){
    echo $i . &quot;: &quot;;
    echo ($i &lt;&lt; 100) &gt;&gt; 97;
    echo &quot;&lt;br&gt;&quot;;
}

However, the results are:

0: 0
1: 8
2: 16
3: 24
4: 32
5: 40
6: 48
7: 56
8: 64
9: 72
10: 80
11: 88
12: 96
13: 104
14: 112
15: 120
16: 128
17: 136
18: 144
19: 152
20: 160
21: 168
22: 176
23: 184
24: 192
25: 200
26: 208
27: 216
28: 224
29: 232
30: 240
31: 248
32: 256
33: 264
34: 272
35: 280
36: 288
37: 296
38: 304
39: 312
40: 320
41: 328
42: 336
43: 344
44: 352
45: 360
46: 368
47: 376
48: 384
49: 392
50: 400
51: 408
52: 416
53: 424
54: 432
55: 440
56: 448
57: 456
58: 464
59: 472
60: 480
61: 488
62: 496
63: 504
64: 512
65: 520
66: 528
67: 536
68: 544
69: 552
70: 560
71: 568
72: 576
73: 584
74: 592
75: 600
76: 608
77: 616
78: 624
79: 632
80: 640
81: 648
82: 656
83: 664
84: 672
85: 680
86: 688
87: 696
88: 704
89: 712
90: 720
91: 728
92: 736
93: 744
94: 752
95: 760
96: 768
97: 776
98: 784
99: 792
100: 800

Can someone explain to me what is going on with the snippet? I know its bitshifting, but I don't understand bitshifting well enough to discern what is going on there. Thank you Bitshifting(位移):有人可以解释一下这段代码是做什么的吗?

答案1

得分: 2

以1为例(你循环遍历0-100,但对于所有数字都是相同的原则)
从括号内开始:

(1 << 100)

将数字1左移100位。这给出了1后面跟着100个零。然后,这个非常大的数字(称为谷歌)向右移动97位,得到:

1000

这是十进制数8的二进制表示。对于其他数字,它们首先被转换为二进制(所以2变成10,3变成11等),然后应用计算。

英文:

Using 1 as an example(you loop through 0-100 but its the same principal for all)
Starting with what is inside the brackets:

(1 &lt;&lt; 100)

will do a left shift on the number 1 by 100 places. this gives you 1 followed by 100 zeros. then, this really large number(known as a googol) gets shifted right by 97 which leaves you with:

1000

which is the binary representation of the decimal number 8. For the other numbers, they are converted to binary first(so 2 becomes 10, 3 becomes 11 etc..) and the calculation is applied.

huangapple
  • 本文由 发表于 2014年2月12日 00:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/21707677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定