example_math_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2021 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. package bits_test
  5. import (
  6. "fmt"
  7. "math/bits"
  8. )
  9. func ExampleAdd32() {
  10. // First number is 33<<32 + 12
  11. n1 := []uint32{33, 12}
  12. // Second number is 21<<32 + 23
  13. n2 := []uint32{21, 23}
  14. // Add them together without producing carry.
  15. d1, carry := bits.Add32(n1[1], n2[1], 0)
  16. d0, _ := bits.Add32(n1[0], n2[0], carry)
  17. nsum := []uint32{d0, d1}
  18. fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  19. // First number is 1<<32 + 2147483648
  20. n1 = []uint32{1, 0x80000000}
  21. // Second number is 1<<32 + 2147483648
  22. n2 = []uint32{1, 0x80000000}
  23. // Add them together producing carry.
  24. d1, carry = bits.Add32(n1[1], n2[1], 0)
  25. d0, _ = bits.Add32(n1[0], n2[0], carry)
  26. nsum = []uint32{d0, d1}
  27. fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  28. // Output:
  29. // [33 12] + [21 23] = [54 35] (carry bit was 0)
  30. // [1 2147483648] + [1 2147483648] = [3 0] (carry bit was 1)
  31. }
  32. func ExampleAdd64() {
  33. // First number is 33<<64 + 12
  34. n1 := []uint64{33, 12}
  35. // Second number is 21<<64 + 23
  36. n2 := []uint64{21, 23}
  37. // Add them together without producing carry.
  38. d1, carry := bits.Add64(n1[1], n2[1], 0)
  39. d0, _ := bits.Add64(n1[0], n2[0], carry)
  40. nsum := []uint64{d0, d1}
  41. fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  42. // First number is 1<<64 + 9223372036854775808
  43. n1 = []uint64{1, 0x8000000000000000}
  44. // Second number is 1<<64 + 9223372036854775808
  45. n2 = []uint64{1, 0x8000000000000000}
  46. // Add them together producing carry.
  47. d1, carry = bits.Add64(n1[1], n2[1], 0)
  48. d0, _ = bits.Add64(n1[0], n2[0], carry)
  49. nsum = []uint64{d0, d1}
  50. fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  51. // Output:
  52. // [33 12] + [21 23] = [54 35] (carry bit was 0)
  53. // [1 9223372036854775808] + [1 9223372036854775808] = [3 0] (carry bit was 1)
  54. }
  55. func ExampleSub32() {
  56. // First number is 33<<32 + 23
  57. n1 := []uint32{33, 23}
  58. // Second number is 21<<32 + 12
  59. n2 := []uint32{21, 12}
  60. // Sub them together without producing carry.
  61. d1, carry := bits.Sub32(n1[1], n2[1], 0)
  62. d0, _ := bits.Sub32(n1[0], n2[0], carry)
  63. nsum := []uint32{d0, d1}
  64. fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  65. // First number is 3<<32 + 2147483647
  66. n1 = []uint32{3, 0x7fffffff}
  67. // Second number is 1<<32 + 2147483648
  68. n2 = []uint32{1, 0x80000000}
  69. // Sub them together producing carry.
  70. d1, carry = bits.Sub32(n1[1], n2[1], 0)
  71. d0, _ = bits.Sub32(n1[0], n2[0], carry)
  72. nsum = []uint32{d0, d1}
  73. fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  74. // Output:
  75. // [33 23] - [21 12] = [12 11] (carry bit was 0)
  76. // [3 2147483647] - [1 2147483648] = [1 4294967295] (carry bit was 1)
  77. }
  78. func ExampleSub64() {
  79. // First number is 33<<64 + 23
  80. n1 := []uint64{33, 23}
  81. // Second number is 21<<64 + 12
  82. n2 := []uint64{21, 12}
  83. // Sub them together without producing carry.
  84. d1, carry := bits.Sub64(n1[1], n2[1], 0)
  85. d0, _ := bits.Sub64(n1[0], n2[0], carry)
  86. nsum := []uint64{d0, d1}
  87. fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  88. // First number is 3<<64 + 9223372036854775807
  89. n1 = []uint64{3, 0x7fffffffffffffff}
  90. // Second number is 1<<64 + 9223372036854775808
  91. n2 = []uint64{1, 0x8000000000000000}
  92. // Sub them together producing carry.
  93. d1, carry = bits.Sub64(n1[1], n2[1], 0)
  94. d0, _ = bits.Sub64(n1[0], n2[0], carry)
  95. nsum = []uint64{d0, d1}
  96. fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
  97. // Output:
  98. // [33 23] - [21 12] = [12 11] (carry bit was 0)
  99. // [3 9223372036854775807] - [1 9223372036854775808] = [1 18446744073709551615] (carry bit was 1)
  100. }
  101. func ExampleMul32() {
  102. // First number is 0<<32 + 12
  103. n1 := []uint32{0, 12}
  104. // Second number is 0<<32 + 12
  105. n2 := []uint32{0, 12}
  106. // Multiply them together without producing overflow.
  107. hi, lo := bits.Mul32(n1[1], n2[1])
  108. nsum := []uint32{hi, lo}
  109. fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
  110. // First number is 0<<32 + 2147483648
  111. n1 = []uint32{0, 0x80000000}
  112. // Second number is 0<<32 + 2
  113. n2 = []uint32{0, 2}
  114. // Multiply them together producing overflow.
  115. hi, lo = bits.Mul32(n1[1], n2[1])
  116. nsum = []uint32{hi, lo}
  117. fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
  118. // Output:
  119. // 12 * 12 = [0 144]
  120. // 2147483648 * 2 = [1 0]
  121. }
  122. func ExampleMul64() {
  123. // First number is 0<<64 + 12
  124. n1 := []uint64{0, 12}
  125. // Second number is 0<<64 + 12
  126. n2 := []uint64{0, 12}
  127. // Multiply them together without producing overflow.
  128. hi, lo := bits.Mul64(n1[1], n2[1])
  129. nsum := []uint64{hi, lo}
  130. fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
  131. // First number is 0<<64 + 9223372036854775808
  132. n1 = []uint64{0, 0x8000000000000000}
  133. // Second number is 0<<64 + 2
  134. n2 = []uint64{0, 2}
  135. // Multiply them together producing overflow.
  136. hi, lo = bits.Mul64(n1[1], n2[1])
  137. nsum = []uint64{hi, lo}
  138. fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
  139. // Output:
  140. // 12 * 12 = [0 144]
  141. // 9223372036854775808 * 2 = [1 0]
  142. }
  143. func ExampleDiv32() {
  144. // First number is 0<<32 + 6
  145. n1 := []uint32{0, 6}
  146. // Second number is 0<<32 + 3
  147. n2 := []uint32{0, 3}
  148. // Divide them together.
  149. quo, rem := bits.Div32(n1[0], n1[1], n2[1])
  150. nsum := []uint32{quo, rem}
  151. fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
  152. // First number is 2<<32 + 2147483648
  153. n1 = []uint32{2, 0x80000000}
  154. // Second number is 0<<32 + 2147483648
  155. n2 = []uint32{0, 0x80000000}
  156. // Divide them together.
  157. quo, rem = bits.Div32(n1[0], n1[1], n2[1])
  158. nsum = []uint32{quo, rem}
  159. fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
  160. // Output:
  161. // [0 6] / 3 = [2 0]
  162. // [2 2147483648] / 2147483648 = [5 0]
  163. }
  164. func ExampleDiv64() {
  165. // First number is 0<<64 + 6
  166. n1 := []uint64{0, 6}
  167. // Second number is 0<<64 + 3
  168. n2 := []uint64{0, 3}
  169. // Divide them together.
  170. quo, rem := bits.Div64(n1[0], n1[1], n2[1])
  171. nsum := []uint64{quo, rem}
  172. fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
  173. // First number is 2<<64 + 9223372036854775808
  174. n1 = []uint64{2, 0x8000000000000000}
  175. // Second number is 0<<64 + 9223372036854775808
  176. n2 = []uint64{0, 0x8000000000000000}
  177. // Divide them together.
  178. quo, rem = bits.Div64(n1[0], n1[1], n2[1])
  179. nsum = []uint64{quo, rem}
  180. fmt.Printf("[%v %v] / %v = %v\n", n1[0], n1[1], n2[1], nsum)
  181. // Output:
  182. // [0 6] / 3 = [2 0]
  183. // [2 9223372036854775808] / 9223372036854775808 = [5 0]
  184. }