Skip to content

bitfield #

Description

bitfield is a module for manipulating arrays of bits, i.e. series of zeroes and ones spread across an array of storage units (unsigned 32-bit integers).

BitField Structure

Bit arrays are stored in data structures called 'BitField'. The structure is 'opaque', i.e. its internals are not available to the end user. This module provides API (functions and methods) for accessing and modifying bit arrays.

fn bf_and #

fn bf_and(input1 BitField, input2 BitField) BitField

bf_and performs logical AND operation on every pair of bits from 'input1' and 'input2' and returns the result as a new array. If inputs differ in size, the tail of the longer one is ignored.

fn bf_not #

fn bf_not(input BitField) BitField

bf_not toggles all bits in a bit array and returns the result as a new array.

fn bf_or #

fn bf_or(input1 BitField, input2 BitField) BitField

bf_or performs logical OR operation on every pair of bits from 'input1' and 'input2' and returns the result as a new array. If inputs differ in size, the tail of the longer one is ignored.

fn bf_xor #

fn bf_xor(input1 BitField, input2 BitField) BitField

bf_xor perform logical XOR operation on every pair of bits from 'input1' and 'input2' and returns the result as a new array. If inputs differ in size, the tail of the longer one is ignored.

fn from_bytes #

fn from_bytes(input []u8) BitField

from_bytes converts a byte array into a bitfield. [0x0F, 0x01] => 0000 1111 0000 0001

fn from_bytes_lowest_bits_first #

fn from_bytes_lowest_bits_first(input []u8) BitField

from_bytes_lowest_bits_first converts a byte array into a bitfield [0x0F, 0x01] => 1111 0000 1000 0000

fn from_str #

fn from_str(input string) BitField

from_str converts a string of characters ('0' and '1') to a bit array. Any character different from '0' is treated as '1'.

fn hamming #

fn hamming(input1 BitField, input2 BitField) int

hamming computes the Hamming distance between two bit arrays.

fn join #

fn join(input1 BitField, input2 BitField) BitField

join concatenates two bit arrays and returns the result as a new array.

fn new #

fn new(size int) BitField

new creates an empty bit array capable of storing 'size' bits.

struct BitField #

struct BitField {
mut:
	size int
	// field *u32
	field []u32
}

fn (BitField) str #

fn (input BitField) str() string

str converts the bit array to a string of characters ('0' and '1') and return the string

fn (BitField) free #

unsafe
fn (instance &BitField) free()

frees the memory allocated for the bitfield instance

fn (BitField) get_bit #

fn (instance BitField) get_bit(bitnr int) int

get_bit returns the value (0 or 1) of bit number 'bit_nr' (count from 0).

fn (BitField) set_bit #

fn (mut instance BitField) set_bit(bitnr int)

set_bit sets bit number 'bit_nr' to 1 (count from 0).

fn (BitField) clear_bit #

fn (mut instance BitField) clear_bit(bitnr int)

clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0).

fn (BitField) extract #

fn (instance BitField) extract(start int, len int) u64

extract returns the value converted from a slice of bit numbers from 'start' by the length of 'len'. 0101 (1, 2) => 0b10

fn (BitField) insert #

fn (mut instance BitField) insert[T](start int, len int, _value T)

insert sets bit numbers from 'start' to 'len' length with the value converted from the number 'value'. 0000 (1, 2, 0b10) => 0100

fn (BitField) extract_lowest_bits_first #

fn (instance BitField) extract_lowest_bits_first(start int, len int) u64

extract returns the value converted from a slice of bit numbers from 'start' by the length of 'len'. 0101 (1, 2) => 0b01

fn (BitField) insert_lowest_bits_first #

fn (mut instance BitField) insert_lowest_bits_first[T](start int, len int, _value T)

insert sets bit numbers from 'start' to 'len' length with the value converted from the number 'value'. 0000 (1, 2, 0b10) => 0010

fn (BitField) set_all #

fn (mut instance BitField) set_all()

set_all sets all bits in the array to 1.

fn (BitField) clear_all #

fn (mut instance BitField) clear_all()

clear_all clears (sets to zero) all bits in the array.

fn (BitField) toggle_bit #

fn (mut instance BitField) toggle_bit(bitnr int)

toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit number 'bit_nr'.

fn (BitField) set_if #

fn (mut instance BitField) set_if(cond bool, bitnr int)

set_if sets bit number 'bit_nr' to 1 (count from 0) if cond is true or clear the bit.

fn (BitField) toggle_bits #

fn (mut instance BitField) toggle_bits(a ...int)

toggle_bits changes the value (from 0 to 1 or from 1 to 0) of bits

Example

toggle_bits(1,3,5,7)

fn (BitField) set_bits #

fn (mut instance BitField) set_bits(a ...int)

set_bits sets multiple bits in the array to 1.

Example

set_bits(1,3,5,7)

fn (BitField) clear_bits #

fn (mut instance BitField) clear_bits(a ...int)

clear_bits clear multiple bits in the array to 0.

Example

clear_bits(1,3,5,7)

fn (BitField) has #

fn (mut instance BitField) has(a ...int) bool

has test if at least one of the bits is set

Example

has(1,3,5,7)

fn (BitField) all #

fn (mut instance BitField) all(a ...int) bool

all test if all of the bits are set

Example

all(1,3,5,7)

fn (BitField) get_size #

fn (instance BitField) get_size() int

get_size returns the number of bits the array can hold.

fn (BitField) clone #

fn (instance BitField) clone() BitField

clone creates a copy of a bit array.

fn (BitField) == #

fn (a BitField) == (b BitField) bool

== compares 2 bitfields, and returns true when they are equal

fn (BitField) pop_count #

fn (instance BitField) pop_count() int

pop_count returns the number of set bits (ones) in the array.

fn (BitField) pos #

fn (haystack BitField) pos(needle BitField) int

pos checks if the array contains a sub-array 'needle' and returns its position if it does, -1 if it does not, and -2 on error.

fn (BitField) slice #

fn (input BitField) slice(_start int, _end int) BitField

slice returns a sub-array of bits between 'start_bit_nr' (included) and 'end_bit_nr' (excluded).

fn (BitField) reverse #

fn (instance BitField) reverse() BitField

reverse reverses the order of bits in the array (swap the first with the last, the second with the last but one and so on).

fn (BitField) resize #

fn (mut instance BitField) resize(new_size int)

resize changes the size of the bit array to 'new_size'.

fn (BitField) rotate #

fn (instance BitField) rotate(offset int) BitField

rotate circular-shifts the bits by 'offset' positions (move 'offset' bit to 0, 'offset+1' bit to 1, and so on).