src/integers/bitset

Search:
Group by:
Source   Edit  

Procs

proc `&&`(x`gensym6, y`gensym6: distinct AnyInteger): Integer {.inline.}
Source   Edit  
proc `&&=`(x`gensym0: var Integer; y`gensym0: AnyInteger) {.inline.}
Source   Edit  
func `&<<`(val`gensym26, n`gensym26: distinct AnyInteger): Integer {.inline.}
Source   Edit  
func `&<<=`(val`gensym26: var Integer; n`gensym26: AnyInteger) {.inline.}
Source   Edit  
func `&>>`(val`gensym24, n`gensym24: distinct AnyInteger): Integer {.inline.}
Source   Edit  
func `&>>=`(val`gensym24: var Integer; n`gensym24: AnyInteger) {.inline.}
Source   Edit  
proc `&^`(x`gensym12, y`gensym12: distinct AnyInteger): Integer {.inline.}
Source   Edit  
proc `&^=`(x`gensym4: var Integer; y`gensym4: AnyInteger) {.inline.}
Source   Edit  
proc `&|`(x`gensym9, y`gensym9: distinct AnyInteger): Integer {.inline.}
Source   Edit  
proc `&|=`(x`gensym2: var Integer; y`gensym2: AnyInteger) {.inline.}
Source   Edit  
func `[]`(val: Integer; idx: int): bool {.inline, ...raises: [], tags: [],
    forbids: [].}

Returns the state of the bit at the given index as a boolean.

Uses two's-complement semantics.

Source   Edit  
func `[]=`(n: var Integer; idx: int; bit: bool) {.inline, ...raises: [], tags: [],
    forbids: [].}
Sets the bit of n at the given index idx to 0 (false) or 1 (true). Source   Edit  
func `[]=`(n: var Integer; idx: int; bit: static[bool]) {.inline.}
Sets the bit of n at the given index idx to 0 (false) or 1 (true). Source   Edit  
proc `and`(x`gensym15, y`gensym15: distinct AnyInteger): Integer {.inline.}
Source   Edit  
func bit(_: typedesc[Integer]; idx: SomeInteger): Integer {.inline.}
Returns an Integer consisting of a single 1-bit at the given index. Source   Edit  
func count(val: Integer): int {.inline, ...raises: [], tags: [], forbids: [].}

Returns the number of 1-bits set.

Also known as "popcount" or "Hamming weight."

Example:

assert count(10'gmp) == 2
assert count(0'gmp) == 0
assert count(256'gmp) == 1
Source   Edit  
func nbits(val: Integer): int {.inline, ...raises: [], tags: [], forbids: [].}

Returns the number of bits needed to represent the absolute value of the given integer.

Returns 0 in the special case of zero.

Equivalent to floor(log2(n)) + 1 for positive numbers.

Equivalent to Python's int.bit_length().

Example:

assert nbits(10'gmp) == 4
assert nbits(0'gmp) == 0 # special case
assert nbits(1'gmp) == 1
assert nbits(255'gmp) == 8
assert nbits(256'gmp) == 9
Source   Edit  
func `not`(n: Integer): Integer {.inline, ...raises: [], tags: [], forbids: [].}
Alias for ~. Source   Edit  
proc `or`(x`gensym18, y`gensym18: distinct AnyInteger): Integer {.inline.}
Source   Edit  
proc scanOne(val: Integer; start: int = 0): int {....raises: [], tags: [],
    forbids: [].}

Returns the index of first 1-bit on or after the given optional index (defaults to 0).

Indices run from least significant to most significant.

Returns -1 if no bits are set after the given index.

Note that negative numbers behave as if they have an infinite number of 1-bits.

Example:

assert 11.scanOne() == 0
assert 8.scanOne() == 3
assert 4.scanOne(3) == -1
assert (-2).scanOne(100) == 100 # 0b..∞..11111110
Source   Edit  
proc scanZero(val: Integer; start: int = 0): int {....raises: [], tags: [],
    forbids: [].}

Returns the index of first 0-bit on or after the given optional index (defaults to 0).

Indices run from least significant to most significant.

Returns -1 if all bits are set after the given index (can only happen with negative numbers).

Example:

assert 11.scanZero() == 2
assert 8.scanZero(1) == 1
assert (-1).scanZero() == -1
Source   Edit  
func setNot(n: var Integer) {.inline, ...raises: [], tags: [], forbids: [].}
In-place bitwise NOT operation. Source   Edit  
func `shl`(val`gensym26: Integer; n`gensym26: AnyInteger): Integer {.inline.}
Source   Edit  
func `shr`(val`gensym24: Integer; n`gensym24: AnyInteger): Integer {.inline.}
Source   Edit  
func size(val: Integer): int {.inline, ...raises: [], tags: [], forbids: [].}
Alias for nbits. Source   Edit  
proc `xor`(x`gensym21, y`gensym21: distinct AnyInteger): Integer {.inline.}
Source   Edit  
func `~`(val: Integer): Integer {.inline, ...raises: [], tags: [], forbids: [].}

Bitwise NOT.

Note that this is equivalent to -(x+1) as GMP integers' behave as if they are two's-complement of infinite size.

Source   Edit  

Iterators

iterator items(n: Integer): int {.inline, ...raises: [], tags: [], forbids: [].}

Iterates over the positions of 1-bits in the given Integer n, going from least significant to most significant.

For example for i in 10'gmp would repeat the loop body twice, first with i=1 followed by i=3, as 10 = 2^1 + 2^3.

Note: Giving a negative number yielsds an infinte loop of ever-increasing indices, negative numbers act as if they're two's-complement of infinite size.
Source   Edit