bitops.html 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/bitops</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/bitops</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li>
  50. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  51. </li>
  52. <li>
  53. <details open>
  54. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  55. <ul class="simple simple-toc-section">
  56. <li><a class="reference" href="#BitsRange" title="BitsRange[T] = range[0 .. sizeof(T) * 8 - 1]">BitsRange</a></li>
  57. </ul>
  58. </details>
  59. </li>
  60. <li>
  61. <details open>
  62. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  63. <ul class="simple simple-toc-section">
  64. <ul class="simple nested-toc-section">bitnot
  65. <li><a class="reference" href="#bitnot%2CT" title="bitnot[T: SomeInteger](x: T): T">bitnot[T: SomeInteger](x: T): T</a></li>
  66. </ul>
  67. <ul class="simple nested-toc-section">bitslice
  68. <li><a class="reference" href="#bitslice%2CT%2CSlice%5Bint%5D" title="bitslice[T: SomeInteger](v: var T; slice: Slice[int])">bitslice[T: SomeInteger](v: var T; slice: Slice[int])</a></li>
  69. </ul>
  70. <ul class="simple nested-toc-section">bitsliced
  71. <li><a class="reference" href="#bitsliced%2CT%2CSlice%5Bint%5D" title="bitsliced[T: SomeInteger](v: T; slice: Slice[int]): T">bitsliced[T: SomeInteger](v: T; slice: Slice[int]): T</a></li>
  72. </ul>
  73. <ul class="simple nested-toc-section">clearBit
  74. <li><a class="reference" href="#clearBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D" title="clearBit[T: SomeInteger](v: var T; bit: BitsRange[T])">clearBit[T: SomeInteger](v: var T; bit: BitsRange[T])</a></li>
  75. </ul>
  76. <ul class="simple nested-toc-section">clearMask
  77. <li><a class="reference" href="#clearMask%2CT%2CT" title="clearMask[T: SomeInteger](v: var T; mask: T)">clearMask[T: SomeInteger](v: var T; mask: T)</a></li>
  78. <li><a class="reference" href="#clearMask%2CT%2CSlice%5Bint%5D" title="clearMask[T: SomeInteger](v: var T; slice: Slice[int])">clearMask[T: SomeInteger](v: var T; slice: Slice[int])</a></li>
  79. </ul>
  80. <ul class="simple nested-toc-section">clearMasked
  81. <li><a class="reference" href="#clearMasked%2CT%2CT" title="clearMasked[T: SomeInteger](v, mask: T): T">clearMasked[T: SomeInteger](v, mask: T): T</a></li>
  82. <li><a class="reference" href="#clearMasked%2CT%2CSlice%5Bint%5D" title="clearMasked[T: SomeInteger](v: T; slice: Slice[int]): T">clearMasked[T: SomeInteger](v: T; slice: Slice[int]): T</a></li>
  83. </ul>
  84. <ul class="simple nested-toc-section">countLeadingZeroBits
  85. <li><a class="reference" href="#countLeadingZeroBits%2CSomeInteger" title="countLeadingZeroBits(x: SomeInteger): int">countLeadingZeroBits(x: SomeInteger): int</a></li>
  86. </ul>
  87. <ul class="simple nested-toc-section">countSetBits
  88. <li><a class="reference" href="#countSetBits%2CSomeInteger" title="countSetBits(x: SomeInteger): int">countSetBits(x: SomeInteger): int</a></li>
  89. </ul>
  90. <ul class="simple nested-toc-section">countTrailingZeroBits
  91. <li><a class="reference" href="#countTrailingZeroBits%2CSomeInteger" title="countTrailingZeroBits(x: SomeInteger): int">countTrailingZeroBits(x: SomeInteger): int</a></li>
  92. </ul>
  93. <ul class="simple nested-toc-section">fastLog2
  94. <li><a class="reference" href="#fastLog2%2CSomeInteger" title="fastLog2(x: SomeInteger): int">fastLog2(x: SomeInteger): int</a></li>
  95. </ul>
  96. <ul class="simple nested-toc-section">firstSetBit
  97. <li><a class="reference" href="#firstSetBit%2CSomeInteger" title="firstSetBit(x: SomeInteger): int">firstSetBit(x: SomeInteger): int</a></li>
  98. </ul>
  99. <ul class="simple nested-toc-section">flipBit
  100. <li><a class="reference" href="#flipBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D" title="flipBit[T: SomeInteger](v: var T; bit: BitsRange[T])">flipBit[T: SomeInteger](v: var T; bit: BitsRange[T])</a></li>
  101. </ul>
  102. <ul class="simple nested-toc-section">flipMask
  103. <li><a class="reference" href="#flipMask%2CT%2CT" title="flipMask[T: SomeInteger](v: var T; mask: T)">flipMask[T: SomeInteger](v: var T; mask: T)</a></li>
  104. <li><a class="reference" href="#flipMask%2CT%2CSlice%5Bint%5D" title="flipMask[T: SomeInteger](v: var T; slice: Slice[int])">flipMask[T: SomeInteger](v: var T; slice: Slice[int])</a></li>
  105. </ul>
  106. <ul class="simple nested-toc-section">flipMasked
  107. <li><a class="reference" href="#flipMasked%2CT%2CT" title="flipMasked[T: SomeInteger](v, mask: T): T">flipMasked[T: SomeInteger](v, mask: T): T</a></li>
  108. <li><a class="reference" href="#flipMasked%2CT%2CSlice%5Bint%5D" title="flipMasked[T: SomeInteger](v: T; slice: Slice[int]): T">flipMasked[T: SomeInteger](v: T; slice: Slice[int]): T</a></li>
  109. </ul>
  110. <ul class="simple nested-toc-section">mask
  111. <li><a class="reference" href="#mask%2CT%2CT" title="mask[T: SomeInteger](v: var T; mask: T)">mask[T: SomeInteger](v: var T; mask: T)</a></li>
  112. <li><a class="reference" href="#mask%2CT%2CSlice%5Bint%5D" title="mask[T: SomeInteger](v: var T; slice: Slice[int])">mask[T: SomeInteger](v: var T; slice: Slice[int])</a></li>
  113. </ul>
  114. <ul class="simple nested-toc-section">masked
  115. <li><a class="reference" href="#masked%2CT%2CT" title="masked[T: SomeInteger](v, mask: T): T">masked[T: SomeInteger](v, mask: T): T</a></li>
  116. <li><a class="reference" href="#masked%2CT%2CSlice%5Bint%5D" title="masked[T: SomeInteger](v: T; slice: Slice[int]): T">masked[T: SomeInteger](v: T; slice: Slice[int]): T</a></li>
  117. </ul>
  118. <ul class="simple nested-toc-section">parityBits
  119. <li><a class="reference" href="#parityBits%2CSomeInteger" title="parityBits(x: SomeInteger): int">parityBits(x: SomeInteger): int</a></li>
  120. </ul>
  121. <ul class="simple nested-toc-section">popcount
  122. <li><a class="reference" href="#popcount%2CSomeInteger" title="popcount(x: SomeInteger): int">popcount(x: SomeInteger): int</a></li>
  123. </ul>
  124. <ul class="simple nested-toc-section">reverseBits
  125. <li><a class="reference" href="#reverseBits%2CT" title="reverseBits[T: SomeUnsignedInt](x: T): T">reverseBits[T: SomeUnsignedInt](x: T): T</a></li>
  126. </ul>
  127. <ul class="simple nested-toc-section">rotateLeftBits
  128. <li><a class="reference" href="#rotateLeftBits%2CT%2Crange%5B%5D" title="rotateLeftBits[T: SomeUnsignedInt](value: T; shift: range[0 .. (sizeof(T) * 8)]): T">rotateLeftBits[T: SomeUnsignedInt](value: T; shift: range[0 .. (sizeof(T) * 8)]): T</a></li>
  129. </ul>
  130. <ul class="simple nested-toc-section">rotateRightBits
  131. <li><a class="reference" href="#rotateRightBits%2CT%2Crange%5B%5D" title="rotateRightBits[T: SomeUnsignedInt](value: T; shift: range[0 .. (sizeof(T) * 8)]): T">rotateRightBits[T: SomeUnsignedInt](value: T; shift: range[0 .. (sizeof(T) * 8)]): T</a></li>
  132. </ul>
  133. <ul class="simple nested-toc-section">setBit
  134. <li><a class="reference" href="#setBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D" title="setBit[T: SomeInteger](v: var T; bit: BitsRange[T])">setBit[T: SomeInteger](v: var T; bit: BitsRange[T])</a></li>
  135. </ul>
  136. <ul class="simple nested-toc-section">setMask
  137. <li><a class="reference" href="#setMask%2CT%2CT" title="setMask[T: SomeInteger](v: var T; mask: T)">setMask[T: SomeInteger](v: var T; mask: T)</a></li>
  138. <li><a class="reference" href="#setMask%2CT%2CSlice%5Bint%5D" title="setMask[T: SomeInteger](v: var T; slice: Slice[int])">setMask[T: SomeInteger](v: var T; slice: Slice[int])</a></li>
  139. </ul>
  140. <ul class="simple nested-toc-section">setMasked
  141. <li><a class="reference" href="#setMasked%2CT%2CT" title="setMasked[T: SomeInteger](v, mask: T): T">setMasked[T: SomeInteger](v, mask: T): T</a></li>
  142. <li><a class="reference" href="#setMasked%2CT%2CSlice%5Bint%5D" title="setMasked[T: SomeInteger](v: T; slice: Slice[int]): T">setMasked[T: SomeInteger](v: T; slice: Slice[int]): T</a></li>
  143. </ul>
  144. <ul class="simple nested-toc-section">testBit
  145. <li><a class="reference" href="#testBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D" title="testBit[T: SomeInteger](v: T; bit: BitsRange[T]): bool">testBit[T: SomeInteger](v: T; bit: BitsRange[T]): bool</a></li>
  146. </ul>
  147. <ul class="simple nested-toc-section">toMask
  148. <li><a class="reference" href="#toMask%2CSlice%5Bint%5D" title="toMask[T: SomeInteger](slice: Slice[int]): T">toMask[T: SomeInteger](slice: Slice[int]): T</a></li>
  149. </ul>
  150. </ul>
  151. </details>
  152. </li>
  153. <li>
  154. <details open>
  155. <summary><a class="reference reference-toplevel" href="#17" id="67">Macros</a></summary>
  156. <ul class="simple simple-toc-section">
  157. <ul class="simple nested-toc-section">bitand
  158. <li><a class="reference" href="#bitand.m%2CT%2CT%2Cvarargs%5BT%5D" title="bitand[T: SomeInteger](x, y: T; z: varargs[T]): T">bitand[T: SomeInteger](x, y: T; z: varargs[T]): T</a></li>
  159. </ul>
  160. <ul class="simple nested-toc-section">bitor
  161. <li><a class="reference" href="#bitor.m%2CT%2CT%2Cvarargs%5BT%5D" title="bitor[T: SomeInteger](x, y: T; z: varargs[T]): T">bitor[T: SomeInteger](x, y: T; z: varargs[T]): T</a></li>
  162. </ul>
  163. <ul class="simple nested-toc-section">bitxor
  164. <li><a class="reference" href="#bitxor.m%2CT%2CT%2Cvarargs%5BT%5D" title="bitxor[T: SomeInteger](x, y: T; z: varargs[T]): T">bitxor[T: SomeInteger](x, y: T; z: varargs[T]): T</a></li>
  165. </ul>
  166. <ul class="simple nested-toc-section">clearBits
  167. <li><a class="reference" href="#clearBits.m%2Ctyped%2Cvarargs%5Btyped%5D" title="clearBits(v: typed; bits: varargs[typed]): untyped">clearBits(v: typed; bits: varargs[typed]): untyped</a></li>
  168. </ul>
  169. <ul class="simple nested-toc-section">flipBits
  170. <li><a class="reference" href="#flipBits.m%2Ctyped%2Cvarargs%5Btyped%5D" title="flipBits(v: typed; bits: varargs[typed]): untyped">flipBits(v: typed; bits: varargs[typed]): untyped</a></li>
  171. </ul>
  172. <ul class="simple nested-toc-section">setBits
  173. <li><a class="reference" href="#setBits.m%2Ctyped%2Cvarargs%5Btyped%5D" title="setBits(v: typed; bits: varargs[typed]): untyped">setBits(v: typed; bits: varargs[typed]): untyped</a></li>
  174. </ul>
  175. </ul>
  176. </details>
  177. </li>
  178. </ul>
  179. </div>
  180. <div class="nine columns" id="content">
  181. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  182. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  183. <div id="tocRoot"></div>
  184. <p class="module-desc"><p>This module implements a series of low level methods for bit manipulation.</p>
  185. <p>By default, compiler intrinsics are used where possible to improve performance on supported compilers: <tt class="docutils literal"><span class="pre"><span class="Identifier">GCC</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">LLVM_GCC</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">CLANG</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">VCC</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">ICC</span></span></tt>.</p>
  186. <p>The module will fallback to pure nim procs in case the backend is not supported. You can also use the flag <tt class="docutils literal"><span class="pre"><span class="Identifier">noIntrinsicsBitOpts</span></span></tt> to disable compiler intrinsics.</p>
  187. <p>This module is also compatible with other backends: <tt class="docutils literal"><span class="pre"><span class="Identifier">JavaScript</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">NimScript</span></span></tt> as well as the <tt class="docutils literal"><span class="pre"><span class="Identifier">compiletime</span> <span class="Identifier">VM</span></span></tt>.</p>
  188. <p>As a result of using optimized functions/intrinsics, some functions can return undefined results if the input is invalid. You can use the flag <tt class="docutils literal"><span class="pre"><span class="Identifier">noUndefinedBitOpts</span></span></tt> to force predictable behaviour for all input, causing a small performance hit.</p>
  189. <p>At this time only <tt class="docutils literal"><span class="pre"><span class="Identifier">fastLog2</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">firstSetBit</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">countLeadingZeroBits</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">countTrailingZeroBits</span></span></tt> may return undefined and/or platform dependent values if given invalid input.</p>
  190. </p>
  191. <div class="section" id="6">
  192. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  193. <dl class="item">
  194. <a class="reference external" href="macros.html">macros</a>, <a class="reference external" href="since.html">since</a>, <a class="reference external" href="bitops_utils.html">bitops_utils</a>, <a class="reference external" href="countbits_impl.html">countbits_impl</a>
  195. </dl>
  196. </div>
  197. <div class="section" id="7">
  198. <h1><a class="toc-backref" href="#7">Types</a></h1>
  199. <dl class="item">
  200. <div id="BitsRange">
  201. <dt><pre><a href="bitops.html#BitsRange"><span class="Identifier">BitsRange</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">0</span> <span class="Operator">..</span> <span class="Identifier">sizeof</span><span class="Other">(</span><span class="Identifier">T</span><span class="Other">)</span> <span class="Operator">*</span> <span class="DecNumber">8</span> <span class="Operator">-</span> <span class="DecNumber">1</span><span class="Other">]</span></pre></dt>
  202. <dd>
  203. A range with all bit positions for type <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt>.
  204. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L63" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  205. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L63" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  206. </dd>
  207. </div>
  208. </dl>
  209. </div>
  210. <div class="section" id="12">
  211. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  212. <dl class="item">
  213. <div id="bitnot-procs-all">
  214. <div id="bitnot,T">
  215. <dt><pre><span class="Keyword">func</span> <a href="#bitnot%2CT"><span class="Identifier">bitnot</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">magic</span><span class="Other">:</span> <span class="StringLit">&quot;BitnotI&quot;</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  216. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  217. <dd>
  218. Computes the <tt class="docutils literal"><span class="pre"><span class="Identifier">bitwise</span> <span class="Identifier">complement</span></span></tt> of the integer <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
  219. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L32" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  220. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L32" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  221. </dd>
  222. </div>
  223. </div>
  224. <div id="bitslice-procs-all">
  225. <div id="bitslice,T,Slice[int]">
  226. <dt><pre><span class="Keyword">proc</span> <a href="#bitslice%2CT%2CSlice%5Bint%5D"><span class="Identifier">bitslice</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  227. <dd>
  228. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> into an extracted (and shifted) slice of bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>.
  229. <p><strong class="examples_text">Example:</strong></p>
  230. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="BinNumber">0b101110</span>
  231. <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">bitslice</span><span class="Punctuation">(</span><span class="DecNumber">2</span> <span class="Operator">..</span> <span class="DecNumber">4</span><span class="Punctuation">)</span>
  232. <span class="Identifier">doAssert</span> <span class="Identifier">x</span> <span class="Operator">==</span> <span class="BinNumber">0b011</span></pre>
  233. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L84" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  234. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L84" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  235. </dd>
  236. </div>
  237. </div>
  238. <div id="bitsliced-procs-all">
  239. <div id="bitsliced,T,Slice[int]">
  240. <dt><pre><span class="Keyword">func</span> <a href="#bitsliced%2CT%2CSlice%5Bint%5D"><span class="Identifier">bitsliced</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  241. <dd>
  242. Returns an extracted (and shifted) slice of bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>.
  243. <p><strong class="examples_text">Example:</strong></p>
  244. <pre class="listing"><span class="Identifier">doAssert</span> <span class="BinNumber">0b10111</span><span class="Operator">.</span><span class="Identifier">bitsliced</span><span class="Punctuation">(</span><span class="DecNumber">2</span> <span class="Operator">..</span> <span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b101</span>
  245. <span class="Identifier">doAssert</span> <span class="BinNumber">0b11100</span><span class="Operator">.</span><span class="Identifier">bitsliced</span><span class="Punctuation">(</span><span class="DecNumber">0</span> <span class="Operator">..</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b100</span>
  246. <span class="Identifier">doAssert</span> <span class="BinNumber">0b11100</span><span class="Operator">.</span><span class="Identifier">bitsliced</span><span class="Punctuation">(</span><span class="DecNumber">0</span> <span class="Operator">..&lt;</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b100</span></pre>
  247. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L72" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  248. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L72" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  249. </dd>
  250. </div>
  251. </div>
  252. <div id="clearBit-procs-all">
  253. <div id="clearBit,T,BitsRange[T: SomeInteger]">
  254. <dt><pre><span class="Keyword">proc</span> <a href="#clearBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D"><span class="Identifier">clearBit</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">bit</span><span class="Other">:</span> <a href="bitops.html#BitsRange"><span class="Identifier">BitsRange</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  255. <dd>
  256. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bit at position <tt class="docutils literal"><span class="pre"><span class="Identifier">bit</span></span></tt> set to 0.
  257. <p><strong class="examples_text">Example:</strong></p>
  258. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  259. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearBit</span><span class="Punctuation">(</span><span class="DecNumber">1'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  260. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span></pre>
  261. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L292" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  262. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L292" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  263. </dd>
  264. </div>
  265. </div>
  266. <div id="clearMask-procs-all">
  267. <div id="clearMask,T,T">
  268. <dt><pre><span class="Keyword">proc</span> <a href="#clearMask%2CT%2CT"><span class="Identifier">clearMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  269. <dd>
  270. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> set to 0.</p>
  271. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation with an <em>inverted mask</em>.</p>
  272. <p><strong class="examples_text">Example:</strong></p>
  273. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  274. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearMask</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  275. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span></pre>
  276. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L217" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  277. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L217" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  278. </dd>
  279. </div>
  280. <div id="clearMask,T,Slice[int]">
  281. <dt><pre><span class="Keyword">proc</span> <a href="#clearMask%2CT%2CSlice%5Bint%5D"><span class="Identifier">clearMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  282. <dd>
  283. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> set to 0.</p>
  284. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation with an <em>inverted mask</em>.</p>
  285. <p><strong class="examples_text">Example:</strong></p>
  286. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  287. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearMask</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
  288. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span></pre>
  289. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L229" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  290. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L229" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  291. </dd>
  292. </div>
  293. </div>
  294. <div id="clearMasked-procs-all">
  295. <div id="clearMasked,T,T">
  296. <dt><pre><span class="Keyword">func</span> <a href="#clearMasked%2CT%2CT"><span class="Identifier">clearMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">,</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  297. <dd>
  298. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> set to 0.</p>
  299. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation with an <em>inverted mask</em>.</p>
  300. <p><strong class="examples_text">Example:</strong></p>
  301. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  302. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearMasked</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span></pre>
  303. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L195" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  304. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L195" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  305. </dd>
  306. </div>
  307. <div id="clearMasked,T,Slice[int]">
  308. <dt><pre><span class="Keyword">func</span> <a href="#clearMasked%2CT%2CSlice%5Bint%5D"><span class="Identifier">clearMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  309. <dd>
  310. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> set to 0.</p>
  311. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation with an <em>inverted mask</em>.</p>
  312. <p><strong class="examples_text">Example:</strong></p>
  313. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  314. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearMasked</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span></pre>
  315. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L206" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  316. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L206" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  317. </dd>
  318. </div>
  319. </div>
  320. <div id="countLeadingZeroBits-procs-all">
  321. <div id="countLeadingZeroBits,SomeInteger">
  322. <dt><pre><span class="Keyword">func</span> <a href="#countLeadingZeroBits%2CSomeInteger"><span class="Identifier">countLeadingZeroBits</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  323. <dd>
  324. <p>Returns the number of leading zero bits in an integer. If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is zero, when <tt class="docutils literal"><span class="pre"><span class="Identifier">noUndefinedBitOpts</span></span></tt> is set, the result is 0, otherwise the result is undefined.</p>
  325. <p><strong>See also:</strong></p>
  326. <ul class="simple"><li><a class="reference external" href="#countTrailingZeroBits,SomeInteger">countTrailingZeroBits proc</a></li>
  327. </ul>
  328. <p><strong class="examples_text">Example:</strong></p>
  329. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">countLeadingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">7</span>
  330. <span class="Identifier">doAssert</span> <span class="Identifier">countLeadingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">6</span>
  331. <span class="Identifier">doAssert</span> <span class="Identifier">countLeadingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0100'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
  332. <span class="Identifier">doAssert</span> <span class="Identifier">countLeadingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1000'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span>
  333. <span class="Identifier">doAssert</span> <span class="Identifier">countLeadingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span></pre>
  334. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L604" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  335. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L604" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  336. </dd>
  337. </div>
  338. </div>
  339. <div id="countSetBits-procs-all">
  340. <div id="countSetBits,SomeInteger">
  341. <dt><pre><span class="Keyword">func</span> <a href="#countSetBits%2CSomeInteger"><span class="Identifier">countSetBits</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  342. <dd>
  343. Counts the set bits in an integer (also called <span id="hamming-weight_1">Hamming weight</span>).
  344. <p><strong class="examples_text">Example:</strong></p>
  345. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">countSetBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  346. <span class="Identifier">doAssert</span> <span class="Identifier">countSetBits</span><span class="Punctuation">(</span><span class="BinNumber">0b1010_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span></pre>
  347. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L488" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  348. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L488" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  349. </dd>
  350. </div>
  351. </div>
  352. <div id="countTrailingZeroBits-procs-all">
  353. <div id="countTrailingZeroBits,SomeInteger">
  354. <dt><pre><span class="Keyword">func</span> <a href="#countTrailingZeroBits%2CSomeInteger"><span class="Identifier">countTrailingZeroBits</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  355. <dd>
  356. <p>Returns the number of trailing zeros in an integer. If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is zero, when <tt class="docutils literal"><span class="pre"><span class="Identifier">noUndefinedBitOpts</span></span></tt> is set, the result is 0, otherwise the result is undefined.</p>
  357. <p><strong>See also:</strong></p>
  358. <ul class="simple"><li><a class="reference external" href="#countLeadingZeroBits,SomeInteger">countLeadingZeroBits proc</a></li>
  359. </ul>
  360. <p><strong class="examples_text">Example:</strong></p>
  361. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">countTrailingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
  362. <span class="Identifier">doAssert</span> <span class="Identifier">countTrailingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  363. <span class="Identifier">doAssert</span> <span class="Identifier">countTrailingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0100'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  364. <span class="Identifier">doAssert</span> <span class="Identifier">countTrailingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1000'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
  365. <span class="Identifier">doAssert</span> <span class="Identifier">countTrailingZeroBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span></pre>
  366. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L632" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  367. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L632" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  368. </dd>
  369. </div>
  370. </div>
  371. <div id="fastLog2-procs-all">
  372. <div id="fastLog2,SomeInteger">
  373. <dt><pre><span class="Keyword">func</span> <a href="#fastLog2%2CSomeInteger"><span class="Identifier">fastLog2</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  374. <dd>
  375. Quickly find the log base 2 of an integer. If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is zero, when <tt class="docutils literal"><span class="pre"><span class="Identifier">noUndefinedBitOpts</span></span></tt> is set, the result is -1, otherwise the result is undefined.
  376. <p><strong class="examples_text">Example:</strong></p>
  377. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">fastLog2</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
  378. <span class="Identifier">doAssert</span> <span class="Identifier">fastLog2</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  379. <span class="Identifier">doAssert</span> <span class="Identifier">fastLog2</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0100'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  380. <span class="Identifier">doAssert</span> <span class="Identifier">fastLog2</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1000'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
  381. <span class="Identifier">doAssert</span> <span class="Identifier">fastLog2</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span></pre>
  382. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L565" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  383. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L565" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  384. </dd>
  385. </div>
  386. </div>
  387. <div id="firstSetBit-procs-all">
  388. <div id="firstSetBit,SomeInteger">
  389. <dt><pre><span class="Keyword">func</span> <a href="#firstSetBit%2CSomeInteger"><span class="Identifier">firstSetBit</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  390. <dd>
  391. Returns the 1-based index of the least significant set bit of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>. If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is zero, when <tt class="docutils literal"><span class="pre"><span class="Identifier">noUndefinedBitOpts</span></span></tt> is set, the result is 0, otherwise the result is undefined.
  392. <p><strong class="examples_text">Example:</strong></p>
  393. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">firstSetBit</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  394. <span class="Identifier">doAssert</span> <span class="Identifier">firstSetBit</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
  395. <span class="Identifier">doAssert</span> <span class="Identifier">firstSetBit</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0100'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
  396. <span class="Identifier">doAssert</span> <span class="Identifier">firstSetBit</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1000'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span>
  397. <span class="Identifier">doAssert</span> <span class="Identifier">firstSetBit</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span></pre>
  398. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L522" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  399. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L522" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  400. </dd>
  401. </div>
  402. </div>
  403. <div id="flipBit-procs-all">
  404. <div id="flipBit,T,BitsRange[T: SomeInteger]">
  405. <dt><pre><span class="Keyword">proc</span> <a href="#flipBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D"><span class="Identifier">flipBit</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">bit</span><span class="Other">:</span> <a href="bitops.html#BitsRange"><span class="Identifier">BitsRange</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  406. <dd>
  407. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bit at position <tt class="docutils literal"><span class="pre"><span class="Identifier">bit</span></span></tt> flipped.
  408. <p><strong class="examples_text">Example:</strong></p>
  409. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  410. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipBit</span><span class="Punctuation">(</span><span class="DecNumber">1'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  411. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0001'</span><span class="Identifier">u8</span>
  412. <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  413. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipBit</span><span class="Punctuation">(</span><span class="DecNumber">2'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  414. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0111'</span><span class="Identifier">u8</span></pre>
  415. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L301" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  416. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L301" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  417. </dd>
  418. </div>
  419. </div>
  420. <div id="flipMask-procs-all">
  421. <div id="flipMask,T,T">
  422. <dt><pre><span class="Keyword">proc</span> <a href="#flipMask%2CT%2CT"><span class="Identifier">flipMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  423. <dd>
  424. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> flipped.</p>
  425. <p>Effectively maps to a <a class="reference external" href="#bitxor.m,T,T,varargs[T]">bitxor</a> operation.</p>
  426. <p><strong class="examples_text">Example:</strong></p>
  427. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  428. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipMask</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  429. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1001'</span><span class="Identifier">u8</span></pre>
  430. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L261" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  431. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L261" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  432. </dd>
  433. </div>
  434. <div id="flipMask,T,Slice[int]">
  435. <dt><pre><span class="Keyword">proc</span> <a href="#flipMask%2CT%2CSlice%5Bint%5D"><span class="Identifier">flipMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  436. <dd>
  437. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> flipped.</p>
  438. <p>Effectively maps to a <a class="reference external" href="#bitxor.m,T,T,varargs[T]">bitxor</a> operation.</p>
  439. <p><strong class="examples_text">Example:</strong></p>
  440. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  441. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipMask</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
  442. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1101'</span><span class="Identifier">u8</span></pre>
  443. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L272" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  444. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L272" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  445. </dd>
  446. </div>
  447. </div>
  448. <div id="flipMasked-procs-all">
  449. <div id="flipMasked,T,T">
  450. <dt><pre><span class="Keyword">func</span> <a href="#flipMasked%2CT%2CT"><span class="Identifier">flipMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">,</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  451. <dd>
  452. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> flipped.</p>
  453. <p>Effectively maps to a <a class="reference external" href="#bitxor.m,T,T,varargs[T]">bitxor</a> operation.</p>
  454. <p><strong class="examples_text">Example:</strong></p>
  455. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  456. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipMasked</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1001'</span><span class="Identifier">u8</span></pre>
  457. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L241" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  458. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L241" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  459. </dd>
  460. </div>
  461. <div id="flipMasked,T,Slice[int]">
  462. <dt><pre><span class="Keyword">func</span> <a href="#flipMasked%2CT%2CSlice%5Bint%5D"><span class="Identifier">flipMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  463. <dd>
  464. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> flipped.</p>
  465. <p>Effectively maps to a <a class="reference external" href="#bitxor.m,T,T,varargs[T]">bitxor</a> operation.</p>
  466. <p><strong class="examples_text">Example:</strong></p>
  467. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  468. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipMasked</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1101'</span><span class="Identifier">u8</span></pre>
  469. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L251" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  470. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L251" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  471. </dd>
  472. </div>
  473. </div>
  474. <div id="mask-procs-all">
  475. <div id="mask,T,T">
  476. <dt><pre><span class="Keyword">proc</span> <a href="#mask%2CT%2CT"><span class="Identifier">mask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  477. <dd>
  478. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with only the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> matching those of <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> set to 1.</p>
  479. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation.</p>
  480. <p><strong class="examples_text">Example:</strong></p>
  481. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  482. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">mask</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  483. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span></pre>
  484. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L129" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  485. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L129" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  486. </dd>
  487. </div>
  488. <div id="mask,T,Slice[int]">
  489. <dt><pre><span class="Keyword">proc</span> <a href="#mask%2CT%2CSlice%5Bint%5D"><span class="Identifier">mask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  490. <dd>
  491. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with only the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> matching those of <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> set to 1.</p>
  492. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation.</p>
  493. <p><strong class="examples_text">Example:</strong></p>
  494. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_1011'</span><span class="Identifier">u8</span>
  495. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">mask</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
  496. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span></pre>
  497. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L141" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  498. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L141" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  499. </dd>
  500. </div>
  501. </div>
  502. <div id="masked-procs-all">
  503. <div id="masked,T,T">
  504. <dt><pre><span class="Keyword">proc</span> <a href="#masked%2CT%2CT"><span class="Identifier">masked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">,</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  505. <dd>
  506. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with only the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> matching those of <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> set to 1.</p>
  507. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation.</p>
  508. <p><strong class="examples_text">Example:</strong></p>
  509. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  510. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">masked</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_0010'</span><span class="Identifier">u8</span></pre>
  511. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L107" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  512. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L107" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  513. </dd>
  514. </div>
  515. <div id="masked,T,Slice[int]">
  516. <dt><pre><span class="Keyword">func</span> <a href="#masked%2CT%2CSlice%5Bint%5D"><span class="Identifier">masked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  517. <dd>
  518. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with only the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> matching those of <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> set to 1.</p>
  519. <p>Effectively maps to a <a class="reference external" href="#bitand.m,T,T,varargs[T]">bitand</a> operation.</p>
  520. <p><strong class="examples_text">Example:</strong></p>
  521. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_1011'</span><span class="Identifier">u8</span>
  522. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">masked</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span></pre>
  523. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L118" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  524. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L118" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  525. </dd>
  526. </div>
  527. </div>
  528. <div id="parityBits-procs-all">
  529. <div id="parityBits,SomeInteger">
  530. <dt><pre><span class="Keyword">func</span> <a href="#parityBits%2CSomeInteger"><span class="Identifier">parityBits</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  531. <dd>
  532. Calculate the bit parity in an integer. If the number of 1-bits is odd, the parity is 1, otherwise 0.
  533. <p><strong class="examples_text">Example:</strong></p>
  534. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">parityBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_0000'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
  535. <span class="Identifier">doAssert</span> <span class="Identifier">parityBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0101_0001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  536. <span class="Identifier">doAssert</span> <span class="Identifier">parityBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0110_1001'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
  537. <span class="Identifier">doAssert</span> <span class="Identifier">parityBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0111_1111'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span></pre>
  538. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L500" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  539. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L500" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  540. </dd>
  541. </div>
  542. </div>
  543. <div id="popcount-procs-all">
  544. <div id="popcount,SomeInteger">
  545. <dt><pre><span class="Keyword">func</span> <a href="#popcount%2CSomeInteger"><span class="Identifier">popcount</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  546. <dd>
  547. Alias for <a class="reference external" href="#countSetBits,SomeInteger">countSetBits</a> (Hamming weight).
  548. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L496" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  549. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L496" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  550. </dd>
  551. </div>
  552. </div>
  553. <div id="reverseBits-procs-all">
  554. <div id="reverseBits,T">
  555. <dt><pre><span class="Keyword">func</span> <a href="#reverseBits%2CT"><span class="Identifier">reverseBits</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeUnsignedInt"><span class="Identifier">SomeUnsignedInt</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  556. <dd>
  557. Return the bit reversal of x.
  558. <p><strong class="examples_text">Example:</strong></p>
  559. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">reverseBits</span><span class="Punctuation">(</span><span class="BinNumber">0b10100100'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b00100101'</span><span class="Identifier">u8</span>
  560. <span class="Identifier">doAssert</span> <span class="Identifier">reverseBits</span><span class="Punctuation">(</span><span class="HexNumber">0xdd'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="HexNumber">0xbb'</span><span class="Identifier">u8</span>
  561. <span class="Identifier">doAssert</span> <span class="Identifier">reverseBits</span><span class="Punctuation">(</span><span class="HexNumber">0xddbb'</span><span class="Identifier">u16</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="HexNumber">0xddbb'</span><span class="Identifier">u16</span>
  562. <span class="Identifier">doAssert</span> <span class="Identifier">reverseBits</span><span class="Punctuation">(</span><span class="HexNumber">0xdeadbeef'</span><span class="Identifier">u32</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="HexNumber">0xf77db57b'</span><span class="Identifier">u32</span></pre>
  563. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L848" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  564. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L848" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  565. </dd>
  566. </div>
  567. </div>
  568. <div id="rotateLeftBits-procs-all">
  569. <div id="rotateLeftBits,T,range[]">
  570. <dt><pre><span class="Keyword">func</span> <a href="#rotateLeftBits%2CT%2Crange%5B%5D"><span class="Identifier">rotateLeftBits</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeUnsignedInt"><span class="Identifier">SomeUnsignedInt</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">value</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span>
  571. <span class="Identifier">shift</span><span class="Other">:</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">0</span> <span class="Operator">..</span> <span class="Other">(</span><span class="Identifier">sizeof</span><span class="Other">(</span><span class="Identifier">T</span><span class="Other">)</span> <span class="Operator">*</span> <span class="DecNumber">8</span><span class="Other">)</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.
  572. <span class="Identifier">inline</span>.}</pre></dt>
  573. <dd>
  574. Left-rotate bits in a <tt class="docutils literal"><span class="pre"><span class="Identifier">value</span></span></tt>.
  575. <p><strong class="examples_text">Example:</strong></p>
  576. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">rotateLeftBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0110_1001'</span><span class="Identifier">u8</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b1001_0110'</span><span class="Identifier">u8</span>
  577. <span class="Identifier">doAssert</span> <span class="Identifier">rotateLeftBits</span><span class="Punctuation">(</span><span class="BinNumber">0b00111100_11000011'</span><span class="Identifier">u16</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  578. <span class="BinNumber">0b11000011_00111100'</span><span class="Identifier">u16</span>
  579. <span class="Identifier">doAssert</span> <span class="Identifier">rotateLeftBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000111111110000_1111000000001111'</span><span class="Identifier">u32</span><span class="Punctuation">,</span> <span class="DecNumber">16</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  580. <span class="BinNumber">0b1111000000001111_0000111111110000'</span><span class="Identifier">u32</span>
  581. <span class="Identifier">doAssert</span> <span class="Identifier">rotateLeftBits</span><span class="Punctuation">(</span><span class="BinNumber">0b00000000111111111111111100000000_11111111000000000000000011111111'</span><span class="Identifier">u64</span><span class="Punctuation">,</span> <span class="DecNumber">32</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  582. <span class="BinNumber">0b11111111000000000000000011111111_00000000111111111111111100000000'</span><span class="Identifier">u64</span></pre>
  583. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L785" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  584. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L785" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  585. </dd>
  586. </div>
  587. </div>
  588. <div id="rotateRightBits-procs-all">
  589. <div id="rotateRightBits,T,range[]">
  590. <dt><pre><span class="Keyword">func</span> <a href="#rotateRightBits%2CT%2Crange%5B%5D"><span class="Identifier">rotateRightBits</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeUnsignedInt"><span class="Identifier">SomeUnsignedInt</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">value</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span>
  591. <span class="Identifier">shift</span><span class="Other">:</span> <a href="system.html#range"><span class="Identifier">range</span></a><span class="Other">[</span><span class="DecNumber">0</span> <span class="Operator">..</span> <span class="Other">(</span><span class="Identifier">sizeof</span><span class="Other">(</span><span class="Identifier">T</span><span class="Other">)</span> <span class="Operator">*</span> <span class="DecNumber">8</span><span class="Other">)</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  592. <dd>
  593. Right-rotate bits in a <tt class="docutils literal"><span class="pre"><span class="Identifier">value</span></span></tt>.
  594. <p><strong class="examples_text">Example:</strong></p>
  595. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">rotateRightBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0110_1001'</span><span class="Identifier">u8</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b1001_0110'</span><span class="Identifier">u8</span>
  596. <span class="Identifier">doAssert</span> <span class="Identifier">rotateRightBits</span><span class="Punctuation">(</span><span class="BinNumber">0b00111100_11000011'</span><span class="Identifier">u16</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  597. <span class="BinNumber">0b11000011_00111100'</span><span class="Identifier">u16</span>
  598. <span class="Identifier">doAssert</span> <span class="Identifier">rotateRightBits</span><span class="Punctuation">(</span><span class="BinNumber">0b0000111111110000_1111000000001111'</span><span class="Identifier">u32</span><span class="Punctuation">,</span> <span class="DecNumber">16</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  599. <span class="BinNumber">0b1111000000001111_0000111111110000'</span><span class="Identifier">u32</span>
  600. <span class="Identifier">doAssert</span> <span class="Identifier">rotateRightBits</span><span class="Punctuation">(</span><span class="BinNumber">0b00000000111111111111111100000000_11111111000000000000000011111111'</span><span class="Identifier">u64</span><span class="Punctuation">,</span> <span class="DecNumber">32</span><span class="Punctuation">)</span> <span class="Operator">==</span>
  601. <span class="BinNumber">0b11111111000000000000000011111111_00000000111111111111111100000000'</span><span class="Identifier">u64</span></pre>
  602. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L813" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  603. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L813" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  604. </dd>
  605. </div>
  606. </div>
  607. <div id="setBit-procs-all">
  608. <div id="setBit,T,BitsRange[T: SomeInteger]">
  609. <dt><pre><span class="Keyword">proc</span> <a href="#setBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D"><span class="Identifier">setBit</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">bit</span><span class="Other">:</span> <a href="bitops.html#BitsRange"><span class="Identifier">BitsRange</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  610. <dd>
  611. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bit at position <tt class="docutils literal"><span class="pre"><span class="Identifier">bit</span></span></tt> set to 1.
  612. <p><strong class="examples_text">Example:</strong></p>
  613. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  614. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setBit</span><span class="Punctuation">(</span><span class="DecNumber">5'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  615. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0010_0011'</span><span class="Identifier">u8</span></pre>
  616. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L283" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  617. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L283" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  618. </dd>
  619. </div>
  620. </div>
  621. <div id="setMask-procs-all">
  622. <div id="setMask,T,T">
  623. <dt><pre><span class="Keyword">proc</span> <a href="#setMask%2CT%2CT"><span class="Identifier">setMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  624. <dd>
  625. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> set to 1.</p>
  626. <p>Effectively maps to a <a class="reference external" href="#bitor.m,T,T,varargs[T]">bitor</a> operation.</p>
  627. <p><strong class="examples_text">Example:</strong></p>
  628. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  629. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setMask</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span>
  630. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1011'</span><span class="Identifier">u8</span></pre>
  631. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L173" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  632. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L173" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  633. </dd>
  634. </div>
  635. <div id="setMask,T,Slice[int]">
  636. <dt><pre><span class="Keyword">proc</span> <a href="#setMask%2CT%2CSlice%5Bint%5D"><span class="Identifier">setMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  637. <dd>
  638. <p>Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> set to 1.</p>
  639. <p>Effectively maps to a <a class="reference external" href="#bitor.m,T,T,varargs[T]">bitor</a> operation.</p>
  640. <p><strong class="examples_text">Example:</strong></p>
  641. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  642. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setMask</span><span class="Punctuation">(</span><span class="DecNumber">2</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
  643. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span></pre>
  644. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L184" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  645. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L184" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  646. </dd>
  647. </div>
  648. </div>
  649. <div id="setMasked-procs-all">
  650. <div id="setMasked,T,T">
  651. <dt><pre><span class="Keyword">func</span> <a href="#setMasked%2CT%2CT"><span class="Identifier">setMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">,</span> <span class="Identifier">mask</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  652. <dd>
  653. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits from <tt class="docutils literal"><span class="pre"><span class="Identifier">mask</span></span></tt> set to 1.</p>
  654. <p>Effectively maps to a <a class="reference external" href="#bitor.m,T,T,varargs[T]">bitor</a> operation.</p>
  655. <p><strong class="examples_text">Example:</strong></p>
  656. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  657. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setMasked</span><span class="Punctuation">(</span><span class="BinNumber">0b0000_1010'</span><span class="Identifier">u8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1011'</span><span class="Identifier">u8</span></pre>
  658. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L153" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  659. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L153" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  660. </dd>
  661. </div>
  662. <div id="setMasked,T,Slice[int]">
  663. <dt><pre><span class="Keyword">func</span> <a href="#setMasked%2CT%2CSlice%5Bint%5D"><span class="Identifier">setMasked</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  664. <dd>
  665. <p>Returns <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with all the <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span></span></tt> bits in the range of <tt class="docutils literal"><span class="pre"><span class="Identifier">slice</span></span></tt> set to 1.</p>
  666. <p>Effectively maps to a <a class="reference external" href="#bitor.m,T,T,varargs[T]">bitor</a> operation.</p>
  667. <p><strong class="examples_text">Example:</strong></p>
  668. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  669. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setMasked</span><span class="Punctuation">(</span><span class="DecNumber">2</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span></pre>
  670. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L163" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  671. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L163" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  672. </dd>
  673. </div>
  674. </div>
  675. <div id="testBit-procs-all">
  676. <div id="testBit,T,BitsRange[T: SomeInteger]">
  677. <dt><pre><span class="Keyword">proc</span> <a href="#testBit%2CT%2CBitsRange%5BT%3A%20SomeInteger%5D"><span class="Identifier">testBit</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">bit</span><span class="Other">:</span> <a href="bitops.html#BitsRange"><span class="Identifier">BitsRange</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">inline</span>.}</pre></dt>
  678. <dd>
  679. Returns true if the bit in <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt> at positions <tt class="docutils literal"><span class="pre"><span class="Identifier">bit</span></span></tt> is set to 1.
  680. <p><strong class="examples_text">Example:</strong></p>
  681. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span>
  682. <span class="Identifier">doAssert</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">testBit</span><span class="Punctuation">(</span><span class="DecNumber">0</span><span class="Punctuation">)</span>
  683. <span class="Identifier">doAssert</span> <span class="Keyword">not</span> <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">testBit</span><span class="Punctuation">(</span><span class="DecNumber">7</span><span class="Punctuation">)</span></pre>
  684. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L351" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  685. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L351" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  686. </dd>
  687. </div>
  688. </div>
  689. <div id="toMask-procs-all">
  690. <div id="toMask,Slice[int]">
  691. <dt><pre><span class="Keyword">func</span> <a href="#toMask%2CSlice%5Bint%5D"><span class="Identifier">toMask</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">inline</span>.}</pre></dt>
  692. <dd>
  693. Creates a bitmask based on a slice of bits.
  694. <p><strong class="examples_text">Example:</strong></p>
  695. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">toMask</span><span class="Punctuation">[</span><span class="Identifier">int32</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b1110'i32</span>
  696. <span class="Identifier">doAssert</span> <span class="Identifier">toMask</span><span class="Punctuation">[</span><span class="Identifier">int32</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="DecNumber">0</span> <span class="Operator">..</span> <span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="BinNumber">0b1111'i32</span></pre>
  697. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L96" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  698. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L96" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  699. </dd>
  700. </div>
  701. </div>
  702. </dl>
  703. </div>
  704. <div class="section" id="17">
  705. <h1><a class="toc-backref" href="#17">Macros</a></h1>
  706. <dl class="item">
  707. <div id="bitand-macros-all">
  708. <div id="bitand.m,T,T,varargs[T]">
  709. <dt><pre><span class="Keyword">macro</span> <a href="#bitand.m%2CT%2CT%2Cvarargs%5BT%5D"><span class="Identifier">bitand</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">z</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  710. <dd>
  711. Computes the <tt class="docutils literal"><span class="pre"><span class="Identifier">bitwise</span> <span class="Keyword">and</span></span></tt> of all arguments collectively.
  712. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L41" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  713. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L41" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  714. </dd>
  715. </div>
  716. </div>
  717. <div id="bitor-macros-all">
  718. <div id="bitor.m,T,T,varargs[T]">
  719. <dt><pre><span class="Keyword">macro</span> <a href="#bitor.m%2CT%2CT%2Cvarargs%5BT%5D"><span class="Identifier">bitor</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">z</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  720. <dd>
  721. Computes the <tt class="docutils literal"><span class="pre"><span class="Identifier">bitwise</span> <span class="Keyword">or</span></span></tt> of all arguments collectively.
  722. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L48" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  723. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L48" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  724. </dd>
  725. </div>
  726. </div>
  727. <div id="bitxor-macros-all">
  728. <div id="bitxor.m,T,T,varargs[T]">
  729. <dt><pre><span class="Keyword">macro</span> <a href="#bitxor.m%2CT%2CT%2Cvarargs%5BT%5D"><span class="Identifier">bitxor</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">z</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  730. <dd>
  731. Computes the <tt class="docutils literal"><span class="pre"><span class="Identifier">bitwise</span> <span class="Keyword">xor</span></span></tt> of all arguments collectively.
  732. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L55" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  733. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L55" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  734. </dd>
  735. </div>
  736. </div>
  737. <div id="clearBits-macros-all">
  738. <div id="clearBits.m,typed,varargs[typed]">
  739. <dt><pre><span class="Keyword">macro</span> <a href="#clearBits.m%2Ctyped%2Cvarargs%5Btyped%5D"><span class="Identifier">clearBits</span></a><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">;</span> <span class="Identifier">bits</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
  740. <dd>
  741. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bits at positions <tt class="docutils literal"><span class="pre"><span class="Identifier">bits</span></span></tt> set to 0.
  742. <p><strong class="examples_text">Example:</strong></p>
  743. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b1111_1111'</span><span class="Identifier">u8</span>
  744. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">clearBits</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">)</span>
  745. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b0101_0101'</span><span class="Identifier">u8</span></pre>
  746. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L326" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  747. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L326" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  748. </dd>
  749. </div>
  750. </div>
  751. <div id="flipBits-macros-all">
  752. <div id="flipBits.m,typed,varargs[typed]">
  753. <dt><pre><span class="Keyword">macro</span> <a href="#flipBits.m%2Ctyped%2Cvarargs%5Btyped%5D"><span class="Identifier">flipBits</span></a><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">;</span> <span class="Identifier">bits</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
  754. <dd>
  755. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bits at positions <tt class="docutils literal"><span class="pre"><span class="Identifier">bits</span></span></tt> set to 0.
  756. <p><strong class="examples_text">Example:</strong></p>
  757. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_1111'</span><span class="Identifier">u8</span>
  758. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">flipBits</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">)</span>
  759. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b1010_0101'</span><span class="Identifier">u8</span></pre>
  760. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L338" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  761. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L338" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  762. </dd>
  763. </div>
  764. </div>
  765. <div id="setBits-macros-all">
  766. <div id="setBits.m,typed,varargs[typed]">
  767. <dt><pre><span class="Keyword">macro</span> <a href="#setBits.m%2Ctyped%2Cvarargs%5Btyped%5D"><span class="Identifier">setBits</span></a><span class="Other">(</span><span class="Identifier">v</span><span class="Other">:</span> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">;</span> <span class="Identifier">bits</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
  768. <dd>
  769. Mutates <tt class="docutils literal"><span class="pre"><span class="Identifier">v</span></span></tt>, with the bits at positions <tt class="docutils literal"><span class="pre"><span class="Identifier">bits</span></span></tt> set to 1.
  770. <p><strong class="examples_text">Example:</strong></p>
  771. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">v</span> <span class="Operator">=</span> <span class="BinNumber">0b0000_0011'</span><span class="Identifier">u8</span>
  772. <span class="Identifier">v</span><span class="Operator">.</span><span class="Identifier">setBits</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">)</span>
  773. <span class="Identifier">doAssert</span> <span class="Identifier">v</span> <span class="Operator">==</span> <span class="BinNumber">0b1010_1011'</span><span class="Identifier">u8</span></pre>
  774. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/bitops.nim#L314" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  775. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/bitops.nim#L314" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  776. </dd>
  777. </div>
  778. </div>
  779. </dl>
  780. </div>
  781. </div>
  782. </div>
  783. <div class="twelve-columns footer">
  784. <span class="nim-sprite"></span>
  785. <br>
  786. <small style="color: var(--hint);">Made with Nim. Generated: 2025-02-06 22:28:10 UTC</small>
  787. </div>
  788. </div>
  789. </div>
  790. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  791. </body>
  792. </html>