example_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Code generated by go run make_examples.go. DO NOT EDIT.
  5. package bits_test
  6. import (
  7. "fmt"
  8. "math/bits"
  9. )
  10. func ExampleLeadingZeros8() {
  11. fmt.Printf("LeadingZeros8(%08b) = %d\n", 1, bits.LeadingZeros8(1))
  12. // Output:
  13. // LeadingZeros8(00000001) = 7
  14. }
  15. func ExampleLeadingZeros16() {
  16. fmt.Printf("LeadingZeros16(%016b) = %d\n", 1, bits.LeadingZeros16(1))
  17. // Output:
  18. // LeadingZeros16(0000000000000001) = 15
  19. }
  20. func ExampleLeadingZeros32() {
  21. fmt.Printf("LeadingZeros32(%032b) = %d\n", 1, bits.LeadingZeros32(1))
  22. // Output:
  23. // LeadingZeros32(00000000000000000000000000000001) = 31
  24. }
  25. func ExampleLeadingZeros64() {
  26. fmt.Printf("LeadingZeros64(%064b) = %d\n", 1, bits.LeadingZeros64(1))
  27. // Output:
  28. // LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) = 63
  29. }
  30. func ExampleTrailingZeros8() {
  31. fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14))
  32. // Output:
  33. // TrailingZeros8(00001110) = 1
  34. }
  35. func ExampleTrailingZeros16() {
  36. fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14))
  37. // Output:
  38. // TrailingZeros16(0000000000001110) = 1
  39. }
  40. func ExampleTrailingZeros32() {
  41. fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14))
  42. // Output:
  43. // TrailingZeros32(00000000000000000000000000001110) = 1
  44. }
  45. func ExampleTrailingZeros64() {
  46. fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14))
  47. // Output:
  48. // TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) = 1
  49. }
  50. func ExampleOnesCount() {
  51. fmt.Printf("OnesCount(%b) = %d\n", 14, bits.OnesCount(14))
  52. // Output:
  53. // OnesCount(1110) = 3
  54. }
  55. func ExampleOnesCount8() {
  56. fmt.Printf("OnesCount8(%08b) = %d\n", 14, bits.OnesCount8(14))
  57. // Output:
  58. // OnesCount8(00001110) = 3
  59. }
  60. func ExampleOnesCount16() {
  61. fmt.Printf("OnesCount16(%016b) = %d\n", 14, bits.OnesCount16(14))
  62. // Output:
  63. // OnesCount16(0000000000001110) = 3
  64. }
  65. func ExampleOnesCount32() {
  66. fmt.Printf("OnesCount32(%032b) = %d\n", 14, bits.OnesCount32(14))
  67. // Output:
  68. // OnesCount32(00000000000000000000000000001110) = 3
  69. }
  70. func ExampleOnesCount64() {
  71. fmt.Printf("OnesCount64(%064b) = %d\n", 14, bits.OnesCount64(14))
  72. // Output:
  73. // OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) = 3
  74. }
  75. func ExampleRotateLeft8() {
  76. fmt.Printf("%08b\n", 15)
  77. fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
  78. fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
  79. // Output:
  80. // 00001111
  81. // 00111100
  82. // 11000011
  83. }
  84. func ExampleRotateLeft16() {
  85. fmt.Printf("%016b\n", 15)
  86. fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
  87. fmt.Printf("%016b\n", bits.RotateLeft16(15, -2))
  88. // Output:
  89. // 0000000000001111
  90. // 0000000000111100
  91. // 1100000000000011
  92. }
  93. func ExampleRotateLeft32() {
  94. fmt.Printf("%032b\n", 15)
  95. fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
  96. fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
  97. // Output:
  98. // 00000000000000000000000000001111
  99. // 00000000000000000000000000111100
  100. // 11000000000000000000000000000011
  101. }
  102. func ExampleRotateLeft64() {
  103. fmt.Printf("%064b\n", 15)
  104. fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
  105. fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
  106. // Output:
  107. // 0000000000000000000000000000000000000000000000000000000000001111
  108. // 0000000000000000000000000000000000000000000000000000000000111100
  109. // 1100000000000000000000000000000000000000000000000000000000000011
  110. }
  111. func ExampleReverse8() {
  112. fmt.Printf("%08b\n", 19)
  113. fmt.Printf("%08b\n", bits.Reverse8(19))
  114. // Output:
  115. // 00010011
  116. // 11001000
  117. }
  118. func ExampleReverse16() {
  119. fmt.Printf("%016b\n", 19)
  120. fmt.Printf("%016b\n", bits.Reverse16(19))
  121. // Output:
  122. // 0000000000010011
  123. // 1100100000000000
  124. }
  125. func ExampleReverse32() {
  126. fmt.Printf("%032b\n", 19)
  127. fmt.Printf("%032b\n", bits.Reverse32(19))
  128. // Output:
  129. // 00000000000000000000000000010011
  130. // 11001000000000000000000000000000
  131. }
  132. func ExampleReverse64() {
  133. fmt.Printf("%064b\n", 19)
  134. fmt.Printf("%064b\n", bits.Reverse64(19))
  135. // Output:
  136. // 0000000000000000000000000000000000000000000000000000000000010011
  137. // 1100100000000000000000000000000000000000000000000000000000000000
  138. }
  139. func ExampleReverseBytes16() {
  140. fmt.Printf("%016b\n", 15)
  141. fmt.Printf("%016b\n", bits.ReverseBytes16(15))
  142. // Output:
  143. // 0000000000001111
  144. // 0000111100000000
  145. }
  146. func ExampleReverseBytes32() {
  147. fmt.Printf("%032b\n", 15)
  148. fmt.Printf("%032b\n", bits.ReverseBytes32(15))
  149. // Output:
  150. // 00000000000000000000000000001111
  151. // 00001111000000000000000000000000
  152. }
  153. func ExampleReverseBytes64() {
  154. fmt.Printf("%064b\n", 15)
  155. fmt.Printf("%064b\n", bits.ReverseBytes64(15))
  156. // Output:
  157. // 0000000000000000000000000000000000000000000000000000000000001111
  158. // 0000111100000000000000000000000000000000000000000000000000000000
  159. }
  160. func ExampleLen8() {
  161. fmt.Printf("Len8(%08b) = %d\n", 8, bits.Len8(8))
  162. // Output:
  163. // Len8(00001000) = 4
  164. }
  165. func ExampleLen16() {
  166. fmt.Printf("Len16(%016b) = %d\n", 8, bits.Len16(8))
  167. // Output:
  168. // Len16(0000000000001000) = 4
  169. }
  170. func ExampleLen32() {
  171. fmt.Printf("Len32(%032b) = %d\n", 8, bits.Len32(8))
  172. // Output:
  173. // Len32(00000000000000000000000000001000) = 4
  174. }
  175. func ExampleLen64() {
  176. fmt.Printf("Len64(%064b) = %d\n", 8, bits.Len64(8))
  177. // Output:
  178. // Len64(0000000000000000000000000000000000000000000000000000000000001000) = 4
  179. }