On this page
bitops
This module implements a series of low level methods for bit manipulation.
By default, this module use compiler intrinsics where possible 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 dependent value if given invalid input.
Imports
Types
Procs
-
proc bitnot[T: SomeInteger](x: T): T {...}{.magic: "BitnotI", noSideEffect.} -
Computes the
bitwise complementof the integerx. Source Edit -
proc bitslice[T: SomeInteger](v: var T; slice: Slice[int]) {...}{.inline.} -
Mutates
vinto an extracted (and shifted) slice of bits fromv.Example:
Source Editvar x = 0b101110 x.bitslice(2 .. 4) doAssert x == 0b011 -
proc masked[T: SomeInteger](v, mask: T): T {...}{.inline.} -
Returns
v, with only the1bits frommaskmatching those ofvset to 1.Effectively maps to a
bitandoperation.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.masked(0b0000_1010'u8) == 0b0000_0010'u8 -
proc mask[T: SomeInteger](v: var T; mask: T) {...}{.inline.} -
Mutates
v, with only the1bits frommaskmatching those ofvset to 1.Effectively maps to a
bitandoperation.Example:
Source Editvar v = 0b0000_0011'u8 v.mask(0b0000_1010'u8) doAssert v == 0b0000_0010'u8 -
proc mask[T: SomeInteger](v: var T; slice: Slice[int]) {...}{.inline.} -
Mutates
v, with only the1bits in the range ofslicematching those ofvset to 1.Effectively maps to a
bitandoperation.Example:
Source Editvar v = 0b0000_1011'u8 v.mask(1 .. 3) doAssert v == 0b0000_1010'u8 -
proc setMask[T: SomeInteger](v: var T; mask: T) {...}{.inline.} -
Mutates
v, with all the1bits frommaskset to 1.Effectively maps to a
bitoroperation.Example:
Source Editvar v = 0b0000_0011'u8 v.setMask(0b0000_1010'u8) doAssert v == 0b0000_1011'u8 -
proc setMask[T: SomeInteger](v: var T; slice: Slice[int]) {...}{.inline.} -
Mutates
v, with all the1bits in the range ofsliceset to 1.Effectively maps to a
bitoroperation.Example:
Source Editvar v = 0b0000_0011'u8 v.setMask(2 .. 3) doAssert v == 0b0000_1111'u8 -
proc clearMask[T: SomeInteger](v: var T; mask: T) {...}{.inline.} -
Mutates
v, with all the1bits frommaskset to 0.Effectively maps to a
bitandoperation with an inverted mask.Example:
Source Editvar v = 0b0000_0011'u8 v.clearMask(0b0000_1010'u8) doAssert v == 0b0000_0001'u8 -
proc clearMask[T: SomeInteger](v: var T; slice: Slice[int]) {...}{.inline.} -
Mutates
v, with all the1bits in the range ofsliceset to 0.Effectively maps to a
bitandoperation with an inverted mask.Example:
Source Editvar v = 0b0000_0011'u8 v.clearMask(1 .. 3) doAssert v == 0b0000_0001'u8 -
proc flipMask[T: SomeInteger](v: var T; mask: T) {...}{.inline.} -
Mutates
v, with all the1bits frommaskflipped.Effectively maps to a
bitxoroperation.Example:
Source Editvar v = 0b0000_0011'u8 v.flipMask(0b0000_1010'u8) doAssert v == 0b0000_1001'u8 -
proc flipMask[T: SomeInteger](v: var T; slice: Slice[int]) {...}{.inline.} -
Mutates
v, with all the1bits in the range ofsliceflipped.Effectively maps to a
bitxoroperation.Example:
Source Editvar v = 0b0000_0011'u8 v.flipMask(1 .. 3) doAssert v == 0b0000_1101'u8 -
proc setBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {...}{.inline.} -
Mutates
v, with the bit at positionbitset to 1Example:
Source Editvar v = 0b0000_0011'u8 v.setBit(5'u8) doAssert v == 0b0010_0011'u8 -
proc clearBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {...}{.inline.} -
Mutates
v, with the bit at positionbitset to 0Example:
Source Editvar v = 0b0000_0011'u8 v.clearBit(1'u8) doAssert v == 0b0000_0001'u8 -
proc flipBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {...}{.inline.} -
Mutates
v, with the bit at positionbitflippedExample:
Source Editvar v = 0b0000_0011'u8 v.flipBit(1'u8) doAssert v == 0b0000_0001'u8 v = 0b0000_0011'u8 v.flipBit(2'u8) doAssert v == 0b0000_0111'u8 -
proc testBit[T: SomeInteger](v: T; bit: BitsRange[T]): bool {...}{.inline.} -
Returns true if the bit in
vat positionsbitis set to 1Example:
Source Editvar v = 0b0000_1111'u8 doAssert v.testBit(0) doAssert not v.testBit(7) -
proc countSetBits(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Counts the set bits in integer. (also called Hamming weight.)
Example:
Source EditdoAssert countSetBits(0b0000_0011'u8) == 2 doAssert countSetBits(0b1010_1010'u8) == 4 -
proc popcount(x: SomeInteger): int {...}{.inline, noSideEffect.} - Alias for for countSetBits. (Hamming weight.) Source Edit
-
proc parityBits(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Calculate the bit parity in integer. If number of 1-bit is odd parity is 1, otherwise 0.
Example:
Source EditdoAssert parityBits(0b0000_0000'u8) == 0 doAssert parityBits(0b0101_0001'u8) == 1 doAssert parityBits(0b0110_1001'u8) == 0 doAssert parityBits(0b0111_1111'u8) == 1 -
proc firstSetBit(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Returns the 1-based index of the least significant set bit of x. If
xis zero, whennoUndefinedBitOptsis set, result is 0, otherwise result is undefined.Example:
Source EditdoAssert firstSetBit(0b0000_0001'u8) == 1 doAssert firstSetBit(0b0000_0010'u8) == 2 doAssert firstSetBit(0b0000_0100'u8) == 3 doAssert firstSetBit(0b0000_1000'u8) == 4 doAssert firstSetBit(0b0000_1111'u8) == 1 -
proc fastLog2(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Quickly find the log base 2 of an integer. If
xis zero, whennoUndefinedBitOptsis set, result is -1, otherwise result is undefined.Example:
Source EditdoAssert fastLog2(0b0000_0001'u8) == 0 doAssert fastLog2(0b0000_0010'u8) == 1 doAssert fastLog2(0b0000_0100'u8) == 2 doAssert fastLog2(0b0000_1000'u8) == 3 doAssert fastLog2(0b0000_1111'u8) == 3 -
proc countLeadingZeroBits(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Returns the number of leading zero bits in integer. If
xis zero, whennoUndefinedBitOptsis set, result is 0, otherwise result is undefined.See also:
Example:
Source EditdoAssert countLeadingZeroBits(0b0000_0001'u8) == 7 doAssert countLeadingZeroBits(0b0000_0010'u8) == 6 doAssert countLeadingZeroBits(0b0000_0100'u8) == 5 doAssert countLeadingZeroBits(0b0000_1000'u8) == 4 doAssert countLeadingZeroBits(0b0000_1111'u8) == 4 -
proc countTrailingZeroBits(x: SomeInteger): int {...}{.inline, noSideEffect.} -
Returns the number of trailing zeros in integer. If
xis zero, whennoUndefinedBitOptsis set, result is 0, otherwise result is undefined.See also:
Example:
Source EditdoAssert countTrailingZeroBits(0b0000_0001'u8) == 0 doAssert countTrailingZeroBits(0b0000_0010'u8) == 1 doAssert countTrailingZeroBits(0b0000_0100'u8) == 2 doAssert countTrailingZeroBits(0b0000_1000'u8) == 3 doAssert countTrailingZeroBits(0b0000_1111'u8) == 0 -
proc rotateLeftBits(value: uint8; amount: range[0 .. 8]): uint8 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Left-rotate bits in a 8-bits value.
Example:
Source EditdoAssert rotateLeftBits(0b0000_0001'u8, 1) == 0b0000_0010'u8 doAssert rotateLeftBits(0b0000_0001'u8, 2) == 0b0000_0100'u8 doAssert rotateLeftBits(0b0100_0001'u8, 1) == 0b1000_0010'u8 doAssert rotateLeftBits(0b0100_0001'u8, 2) == 0b0000_0101'u8 -
proc rotateLeftBits(value: uint16; amount: range[0 .. 16]): uint16 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Left-rotate bits in a 16-bits value.
See also:
Source Edit -
proc rotateLeftBits(value: uint32; amount: range[0 .. 32]): uint32 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Left-rotate bits in a 32-bits value.
See also:
Source Edit -
proc rotateLeftBits(value: uint64; amount: range[0 .. 64]): uint64 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Left-rotate bits in a 64-bits value.
See also:
Source Edit -
proc rotateRightBits(value: uint8; amount: range[0 .. 8]): uint8 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Right-rotate bits in a 8-bits value.
Example:
Source EditdoAssert rotateRightBits(0b0000_0001'u8, 1) == 0b1000_0000'u8 doAssert rotateRightBits(0b0000_0001'u8, 2) == 0b0100_0000'u8 doAssert rotateRightBits(0b0100_0001'u8, 1) == 0b1010_0000'u8 doAssert rotateRightBits(0b0100_0001'u8, 2) == 0b0101_0000'u8 -
proc rotateRightBits(value: uint16; amount: range[0 .. 16]): uint16 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Right-rotate bits in a 16-bits value.
See also:
Source Edit -
proc rotateRightBits(value: uint32; amount: range[0 .. 32]): uint32 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Right-rotate bits in a 32-bits value.
See also:
Source Edit -
proc rotateRightBits(value: uint64; amount: range[0 .. 64]): uint64 {...}{.inline, noSideEffect, raises: [], tags: [].} -
Right-rotate bits in a 64-bits value.
See also:
Source Edit -
proc reverseBits[T: SomeUnsignedInt](x: T): T {...}{.noSideEffect.} -
Return the bit reversal of x.
Example:
Source EditdoAssert reverseBits(0b10100100'u8) == 0b00100101'u8 doAssert reverseBits(0xdd'u8) == 0xbb'u8 doAssert reverseBits(0xddbb'u16) == 0xddbb'u16 doAssert reverseBits(0xdeadbeef'u32) == 0xf77db57b'u32
Funcs
-
func bitsliced[T: SomeInteger](v: T; slice: Slice[int]): T {...}{.inline.} -
Returns an extracted (and shifted) slice of bits from
v.Example:
Source EditdoAssert 0b10111.bitsliced(2 .. 4) == 0b101 doAssert 0b11100.bitsliced(0 .. 2) == 0b100 doAssert 0b11100.bitsliced(0 ..< 3) == 0b100 -
func toMask[T: SomeInteger](slice: Slice[int]): T {...}{.inline.} -
Creates a bitmask based on a slice of bits.
Example:
Source EditdoAssert toMask[int32](1 .. 3) == 0b1110'i32 doAssert toMask[int32](0 .. 3) == 0b1111'i32 -
func masked[T: SomeInteger](v: T; slice: Slice[int]): T {...}{.inline.} -
Mutates
v, with only the1bits in the range ofslicematching those ofvset to 1.Effectively maps to a
bitandoperation.Example:
Source Editvar v = 0b0000_1011'u8 doAssert v.masked(1 .. 3) == 0b0000_1010'u8 -
func setMasked[T: SomeInteger](v, mask: T): T {...}{.inline.} -
Returns
v, with all the1bits frommaskset to 1.Effectively maps to a
bitoroperation.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.setMasked(0b0000_1010'u8) == 0b0000_1011'u8 -
func setMasked[T: SomeInteger](v: T; slice: Slice[int]): T {...}{.inline.} -
Returns
v, with all the1bits in the range ofsliceset to 1.Effectively maps to a
bitoroperation.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.setMasked(2 .. 3) == 0b0000_1111'u8 -
func clearMasked[T: SomeInteger](v, mask: T): T {...}{.inline.} -
Returns
v, with all the1bits frommaskset to 0.Effectively maps to a
bitandoperation with an inverted mask.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.clearMasked(0b0000_1010'u8) == 0b0000_0001'u8 -
func clearMasked[T: SomeInteger](v: T; slice: Slice[int]): T {...}{.inline.} -
Returns
v, with all the1bits in the range ofsliceset to 0.Effectively maps to a
bitandoperation with an inverted mask.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.clearMasked(1 .. 3) == 0b0000_0001'u8 -
func flipMasked[T: SomeInteger](v, mask: T): T {...}{.inline.} -
Returns
v, with all the1bits frommaskflipped.Effectively maps to a
bitxoroperation.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.flipMasked(0b0000_1010'u8) == 0b0000_1001'u8 -
func flipMasked[T: SomeInteger](v: T; slice: Slice[int]): T {...}{.inline.} -
Returns
v, with all the1bits in the range ofsliceflipped.Effectively maps to a
bitxoroperation.Example:
Source Editvar v = 0b0000_0011'u8 doAssert v.flipMasked(1 .. 3) == 0b0000_1101'u8
Macros
-
macro bitand[T: SomeInteger](x, y: T; z: varargs[T]): T -
Computes the
bitwise andof all arguments collectively. Source Edit -
macro bitor[T: SomeInteger](x, y: T; z: varargs[T]): T -
Computes the
bitwise orof all arguments collectively. Source Edit -
macro bitxor[T: SomeInteger](x, y: T; z: varargs[T]): T -
Computes the
bitwise xorof all arguments collectively. Source Edit -
macro setBits(v: typed; bits: varargs[typed]): untyped -
Mutates
v, with the bits at positionsbitsset to 1Example:
Source Editvar v = 0b0000_0011'u8 v.setBits(3, 5, 7) doAssert v == 0b1010_1011'u8 -
macro clearBits(v: typed; bits: varargs[typed]): untyped -
Mutates
v, with the bits at positionsbitsset to 0Example:
Source Editvar v = 0b1111_1111'u8 v.clearBits(1, 3, 5, 7) doAssert v == 0b0101_0101'u8 -
macro flipBits(v: typed; bits: varargs[typed]): untyped -
Mutates
v, with the bits at positionsbitsset to 0Example:
Source Editvar v = 0b0000_1111'u8 v.flipBits(1, 3, 5, 7) doAssert v == 0b1010_0101'u8
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/bitops.html