Source Edit

This module implements a series of low level methods for bit manipulation.

By default, compiler intrinsics are used where possible to improve performance on supported compilers: GCC, LLVM_GCC, CLANG, VCC, ICC.

The module will fallback to pure nim procs in case 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 functions/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 and countTrailingZeroBits may return undefined and/or platform dependent values if given invalid input.

Imports

macros, since, bitops_utils, countbits_impl

Types

  1. BitsRange[T] = range[0 .. sizeof(T) * 8 - 1]

A range with all bit positions for type T. Source Edit

Procs

  1. func bitnot[T: SomeInteger](x: T): T {.magic: "BitnotI", ...raises: [], tags: [],
  2. forbids: [].}

Computes the bitwise complement of the integer x. Source Edit

  1. proc bitslice[T: SomeInteger](v: var T; slice: Slice[int]) {.inline.}

Mutates v into an extracted (and shifted) slice of bits from v.

Example:

  1. var x = 0b101110
  2. x.bitslice(2 .. 4)
  3. doAssert x == 0b011

Source Edit

  1. func bitsliced[T: SomeInteger](v: T; slice: Slice[int]): T {.inline.}

Returns an extracted (and shifted) slice of bits from v.

Example:

  1. doAssert 0b10111.bitsliced(2 .. 4) == 0b101
  2. doAssert 0b11100.bitsliced(0 .. 2) == 0b100
  3. doAssert 0b11100.bitsliced(0 ..< 3) == 0b100

Source Edit

  1. proc clearBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.}

Mutates v, with the bit at position bit set to 0.

Example:

  1. var v = 0b0000_0011'u8
  2. v.clearBit(1'u8)
  3. doAssert v == 0b0000_0001'u8

Source Edit

  1. proc clearMask[T: SomeInteger](v: var T; mask: T) {.inline.}

Mutates v, with all the 1 bits from mask set to 0.

Effectively maps to a bitand operation with an inverted mask.

Example:

  1. var v = 0b0000_0011'u8
  2. v.clearMask(0b0000_1010'u8)
  3. doAssert v == 0b0000_0001'u8

Source Edit

  1. proc clearMask[T: SomeInteger](v: var T; slice: Slice[int]) {.inline.}

Mutates v, with all the 1 bits in the range of slice set to 0.

Effectively maps to a bitand operation with an inverted mask.

Example:

  1. var v = 0b0000_0011'u8
  2. v.clearMask(1 .. 3)
  3. doAssert v == 0b0000_0001'u8

Source Edit

  1. func clearMasked[T: SomeInteger](v, mask: T): T {.inline.}

Returns v, with all the 1 bits from mask set to 0.

Effectively maps to a bitand operation with an inverted mask.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.clearMasked(0b0000_1010'u8) == 0b0000_0001'u8

Source Edit

  1. func clearMasked[T: SomeInteger](v: T; slice: Slice[int]): T {.inline.}

Returns v, with all the 1 bits in the range of slice set to 0.

Effectively maps to a bitand operation with an inverted mask.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.clearMasked(1 .. 3) == 0b0000_0001'u8

Source Edit

  1. func countLeadingZeroBits(x: SomeInteger): int {.inline.}

Returns the number of leading zero bits in an integer. If x is zero, when noUndefinedBitOpts is set, the result is 0, otherwise the result is undefined.

See also:

Example:

  1. doAssert countLeadingZeroBits(0b0000_0001'u8) == 7
  2. doAssert countLeadingZeroBits(0b0000_0010'u8) == 6
  3. doAssert countLeadingZeroBits(0b0000_0100'u8) == 5
  4. doAssert countLeadingZeroBits(0b0000_1000'u8) == 4
  5. doAssert countLeadingZeroBits(0b0000_1111'u8) == 4

Source Edit

  1. func countSetBits(x: SomeInteger): int {.inline.}

Counts the set bits in an integer (also called Hamming weight).

Example:

  1. doAssert countSetBits(0b0000_0011'u8) == 2
  2. doAssert countSetBits(0b1010_1010'u8) == 4

Source Edit

  1. func countTrailingZeroBits(x: SomeInteger): int {.inline.}

Returns the number of trailing zeros in an integer. If x is zero, when noUndefinedBitOpts is set, the result is 0, otherwise the result is undefined.

See also:

Example:

  1. doAssert countTrailingZeroBits(0b0000_0001'u8) == 0
  2. doAssert countTrailingZeroBits(0b0000_0010'u8) == 1
  3. doAssert countTrailingZeroBits(0b0000_0100'u8) == 2
  4. doAssert countTrailingZeroBits(0b0000_1000'u8) == 3
  5. doAssert countTrailingZeroBits(0b0000_1111'u8) == 0

Source Edit

  1. func fastLog2(x: SomeInteger): int {.inline.}

Quickly find the log base 2 of an integer. If x is zero, when noUndefinedBitOpts is set, the result is -1, otherwise the result is undefined.

Example:

  1. doAssert fastLog2(0b0000_0001'u8) == 0
  2. doAssert fastLog2(0b0000_0010'u8) == 1
  3. doAssert fastLog2(0b0000_0100'u8) == 2
  4. doAssert fastLog2(0b0000_1000'u8) == 3
  5. doAssert fastLog2(0b0000_1111'u8) == 3

Source Edit

  1. func firstSetBit(x: SomeInteger): int {.inline.}

Returns the 1-based index of the least significant set bit of x. If x is zero, when noUndefinedBitOpts is set, the result is 0, otherwise the result is undefined.

Example:

  1. doAssert firstSetBit(0b0000_0001'u8) == 1
  2. doAssert firstSetBit(0b0000_0010'u8) == 2
  3. doAssert firstSetBit(0b0000_0100'u8) == 3
  4. doAssert firstSetBit(0b0000_1000'u8) == 4
  5. doAssert firstSetBit(0b0000_1111'u8) == 1

Source Edit

  1. proc flipBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.}

Mutates v, with the bit at position bit flipped.

Example:

  1. var v = 0b0000_0011'u8
  2. v.flipBit(1'u8)
  3. doAssert v == 0b0000_0001'u8
  4. v = 0b0000_0011'u8
  5. v.flipBit(2'u8)
  6. doAssert v == 0b0000_0111'u8

Source Edit

  1. proc flipMask[T: SomeInteger](v: var T; mask: T) {.inline.}

Mutates v, with all the 1 bits from mask flipped.

Effectively maps to a bitxor operation.

Example:

  1. var v = 0b0000_0011'u8
  2. v.flipMask(0b0000_1010'u8)
  3. doAssert v == 0b0000_1001'u8

Source Edit

  1. proc flipMask[T: SomeInteger](v: var T; slice: Slice[int]) {.inline.}

Mutates v, with all the 1 bits in the range of slice flipped.

Effectively maps to a bitxor operation.

Example:

  1. var v = 0b0000_0011'u8
  2. v.flipMask(1 .. 3)
  3. doAssert v == 0b0000_1101'u8

Source Edit

  1. func flipMasked[T: SomeInteger](v, mask: T): T {.inline.}

Returns v, with all the 1 bits from mask flipped.

Effectively maps to a bitxor operation.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.flipMasked(0b0000_1010'u8) == 0b0000_1001'u8

Source Edit

  1. func flipMasked[T: SomeInteger](v: T; slice: Slice[int]): T {.inline.}

Returns v, with all the 1 bits in the range of slice flipped.

Effectively maps to a bitxor operation.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.flipMasked(1 .. 3) == 0b0000_1101'u8

Source Edit

  1. proc mask[T: SomeInteger](v: var T; mask: T) {.inline.}

Mutates v, with only the 1 bits from mask matching those of v set to 1.

Effectively maps to a bitand operation.

Example:

  1. var v = 0b0000_0011'u8
  2. v.mask(0b0000_1010'u8)
  3. doAssert v == 0b0000_0010'u8

Source Edit

  1. proc mask[T: SomeInteger](v: var T; slice: Slice[int]) {.inline.}

Mutates v, with only the 1 bits in the range of slice matching those of v set to 1.

Effectively maps to a bitand operation.

Example:

  1. var v = 0b0000_1011'u8
  2. v.mask(1 .. 3)
  3. doAssert v == 0b0000_1010'u8

Source Edit

  1. proc masked[T: SomeInteger](v, mask: T): T {.inline.}

Returns v, with only the 1 bits from mask matching those of v set to 1.

Effectively maps to a bitand operation.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.masked(0b0000_1010'u8) == 0b0000_0010'u8

Source Edit

  1. func masked[T: SomeInteger](v: T; slice: Slice[int]): T {.inline.}

Returns v, with only the 1 bits in the range of slice matching those of v set to 1.

Effectively maps to a bitand operation.

Example:

  1. let v = 0b0000_1011'u8
  2. doAssert v.masked(1 .. 3) == 0b0000_1010'u8

Source Edit

  1. func parityBits(x: SomeInteger): int {.inline.}

Calculate the bit parity in an integer. If the number of 1-bits is odd, the parity is 1, otherwise 0.

Example:

  1. doAssert parityBits(0b0000_0000'u8) == 0
  2. doAssert parityBits(0b0101_0001'u8) == 1
  3. doAssert parityBits(0b0110_1001'u8) == 0
  4. doAssert parityBits(0b0111_1111'u8) == 1

Source Edit

  1. func popcount(x: SomeInteger): int {.inline.}

Alias for countSetBits (Hamming weight). Source Edit

  1. func reverseBits[T: SomeUnsignedInt](x: T): T

Return the bit reversal of x.

Example:

  1. doAssert reverseBits(0b10100100'u8) == 0b00100101'u8
  2. doAssert reverseBits(0xdd'u8) == 0xbb'u8
  3. doAssert reverseBits(0xddbb'u16) == 0xddbb'u16
  4. doAssert reverseBits(0xdeadbeef'u32) == 0xf77db57b'u32

Source Edit

  1. func rotateLeftBits[T: SomeUnsignedInt](value: T;
  2. shift: range[0 .. (sizeof(T) * 8)]): T {.
  3. inline.}

Left-rotate bits in a value.

Example:

  1. doAssert rotateLeftBits(0b0110_1001'u8, 4) == 0b1001_0110'u8
  2. doAssert rotateLeftBits(0b00111100_11000011'u16, 8) ==
  3. 0b11000011_00111100'u16
  4. doAssert rotateLeftBits(0b0000111111110000_1111000000001111'u32, 16) ==
  5. 0b1111000000001111_0000111111110000'u32
  6. doAssert rotateLeftBits(0b00000000111111111111111100000000_11111111000000000000000011111111'u64, 32) ==
  7. 0b11111111000000000000000011111111_00000000111111111111111100000000'u64

Source Edit

  1. func rotateRightBits[T: SomeUnsignedInt](value: T;
  2. shift: range[0 .. (sizeof(T) * 8)]): T {.inline.}

Right-rotate bits in a value.

Example:

  1. doAssert rotateRightBits(0b0110_1001'u8, 4) == 0b1001_0110'u8
  2. doAssert rotateRightBits(0b00111100_11000011'u16, 8) ==
  3. 0b11000011_00111100'u16
  4. doAssert rotateRightBits(0b0000111111110000_1111000000001111'u32, 16) ==
  5. 0b1111000000001111_0000111111110000'u32
  6. doAssert rotateRightBits(0b00000000111111111111111100000000_11111111000000000000000011111111'u64, 32) ==
  7. 0b11111111000000000000000011111111_00000000111111111111111100000000'u64

Source Edit

  1. proc setBit[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.}

Mutates v, with the bit at position bit set to 1.

Example:

  1. var v = 0b0000_0011'u8
  2. v.setBit(5'u8)
  3. doAssert v == 0b0010_0011'u8

Source Edit

  1. proc setMask[T: SomeInteger](v: var T; mask: T) {.inline.}

Mutates v, with all the 1 bits from mask set to 1.

Effectively maps to a bitor operation.

Example:

  1. var v = 0b0000_0011'u8
  2. v.setMask(0b0000_1010'u8)
  3. doAssert v == 0b0000_1011'u8

Source Edit

  1. proc setMask[T: SomeInteger](v: var T; slice: Slice[int]) {.inline.}

Mutates v, with all the 1 bits in the range of slice set to 1.

Effectively maps to a bitor operation.

Example:

  1. var v = 0b0000_0011'u8
  2. v.setMask(2 .. 3)
  3. doAssert v == 0b0000_1111'u8

Source Edit

  1. func setMasked[T: SomeInteger](v, mask: T): T {.inline.}

Returns v, with all the 1 bits from mask set to 1.

Effectively maps to a bitor operation.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.setMasked(0b0000_1010'u8) == 0b0000_1011'u8

Source Edit

  1. func setMasked[T: SomeInteger](v: T; slice: Slice[int]): T {.inline.}

Returns v, with all the 1 bits in the range of slice set to 1.

Effectively maps to a bitor operation.

Example:

  1. let v = 0b0000_0011'u8
  2. doAssert v.setMasked(2 .. 3) == 0b0000_1111'u8

Source Edit

  1. proc testBit[T: SomeInteger](v: T; bit: BitsRange[T]): bool {.inline.}

Returns true if the bit in v at positions bit is set to 1.

Example:

  1. let v = 0b0000_1111'u8
  2. doAssert v.testBit(0)
  3. doAssert not v.testBit(7)

Source Edit

  1. func toMask[T: SomeInteger](slice: Slice[int]): T {.inline.}

Creates a bitmask based on a slice of bits.

Example:

  1. doAssert toMask[int32](1 .. 3) == 0b1110'i32
  2. doAssert toMask[int32](0 .. 3) == 0b1111'i32

Source Edit

Macros

  1. macro bitand[T: SomeInteger](x, y: T; z: varargs[T]): T

Computes the bitwise and of all arguments collectively. Source Edit

  1. macro bitor[T: SomeInteger](x, y: T; z: varargs[T]): T

Computes the bitwise or of all arguments collectively. Source Edit

  1. macro bitxor[T: SomeInteger](x, y: T; z: varargs[T]): T

Computes the bitwise xor of all arguments collectively. Source Edit

  1. macro clearBits(v: typed; bits: varargs[typed]): untyped

Mutates v, with the bits at positions bits set to 0.

Example:

  1. var v = 0b1111_1111'u8
  2. v.clearBits(1, 3, 5, 7)
  3. doAssert v == 0b0101_0101'u8

Source Edit

  1. macro flipBits(v: typed; bits: varargs[typed]): untyped

Mutates v, with the bits at positions bits set to 0.

Example:

  1. var v = 0b0000_1111'u8
  2. v.flipBits(1, 3, 5, 7)
  3. doAssert v == 0b1010_0101'u8

Source Edit

  1. macro setBits(v: typed; bits: varargs[typed]): untyped

Mutates v, with the bits at positions bits set to 1.

Example:

  1. var v = 0b0000_0011'u8
  2. v.setBits(3, 5, 7)
  3. doAssert v == 0b1010_1011'u8

Source Edit