This module implements a series of low level methods for bit manipulation. By default, this module use compiler intrinsics to improve performance on supported compilers: GCC
, LLVM_GCC
, CLANG
, VCC
, ICC
.
The module will fallback to pure nim procs incase the backend is not supported. You can also use the flag noIntrinsicsBitOpts to disable compiler intrinsics.
This module is also compatible with other backends: Javascript
, Nimscript
as well as the compiletime VM
.
As a result of using optimized function/intrinsics some functions can return undefined results if the input is invalid. You can use the flag noUndefinedBitOpts to force predictable behaviour for all input, causing a small performance hit.
At this time only fastLog2, firstSetBit, `countLeadingZeroBits, countTrailingZeroBits may return undefined and/or platform dependant value if given invalid input.
proc countSetBits(x: SomeInteger): int {...}{.inline, nosideeffect.}
proc popcount(x: SomeInteger): int {...}{.inline, nosideeffect.}
proc parityBits(x: SomeInteger): int {...}{.inline, nosideeffect.}
proc firstSetBit(x: SomeInteger): int {...}{.inline, nosideeffect.}
noUndefinedBitOpts
is set, result is 0, otherwise result is undefined. proc fastLog2(x: SomeInteger): int {...}{.inline, nosideeffect.}
noUndefinedBitOpts
is set, result is -1, otherwise result is undefined. proc countLeadingZeroBits(x: SomeInteger): int {...}{.inline, nosideeffect.}
noUndefinedBitOpts
is set, result is 0, otherwise result is undefined. proc countTrailingZeroBits(x: SomeInteger): int {...}{.inline, nosideeffect.}
noUndefinedBitOpts
is set, result is 0, otherwise result is undefined. proc rotateLeftBits(value: uint8; amount: range[0 .. 8]): uint8 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateLeftBits(value: uint16; amount: range[0 .. 16]): uint16 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateLeftBits(value: uint32; amount: range[0 .. 32]): uint32 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateLeftBits(value: uint64; amount: range[0 .. 64]): uint64 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateRightBits(value: uint8; amount: range[0 .. 8]): uint8 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateRightBits(value: uint16; amount: range[0 .. 16]): uint16 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateRightBits(value: uint32; amount: range[0 .. 32]): uint32 {...}{.inline, noSideEffect, raises: [], tags: [].}
proc rotateRightBits(value: uint64; amount: range[0 .. 64]): uint64 {...}{.inline, noSideEffect, raises: [], tags: [].}
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/bitops.html