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

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

Bitshifting: Can someone explain what this code does?

问题

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

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

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

  1. echo (1 << 100) >> 97;

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

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

然而,结果是:

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

  1. 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):

  1. 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:

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

However, the results are:

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

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

  1. 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. (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:

  1. 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:

确定