bitfield #
Description
bitfield is a module for manipulating arrays of bits, i.e. a series of zeroes and ones spread across an array of storage units.
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. It returns the result as a new bitfield. If inputs differ in size, the tail of the longer one is ignored.
fn bf_not #
fn bf_not(bf BitField) BitField
bf_not toggles all bits in a bitfield and returns the result as a new bitfield.
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. It returns the result as a new bitfield. 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. It returns the result as a new bitfield. If inputs differ in size, the tail of the longer one is ignored.
fn from_bytes #
fn from_bytes(bytes []u8) BitField
from_bytes converts a byte array into a bitfield. [0x0F, 0x01] => 0000 1111 0000 0001 Each byte is bit-reversed via a 256-entry LUT (bits.reverse_8).
fn from_bytes_lowest_bits_first #
fn from_bytes_lowest_bits_first(bytes []u8) BitField
from_bytes_lowest_bits_first converts a byte array into a bitfield. For example: [0x0F, 0x01] => 1111 0000 1000 0000
fn from_str #
fn from_str(str string) BitField
from_str converts a string of characters (0 and 1) to a bitfield. 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 bitfields and returns the result as a new bitfield.
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
}
fn (BitField) str #
fn (bf BitField) str() string
str converts the bit array to a string of characters (0 and 1).
fn (BitField) free #
fn (bf &BitField) free()
free frees the memory allocated for a bitfield.
fn (BitField) get_bit #
fn (bf BitField) get_bit(bitnr int) int
get_bit returns the value (0 or 1) of bit number bitnr (count from 0).
fn (BitField) set_bit #
fn (mut bf BitField) set_bit(bitnr int)
set_bit sets bit number bitnr to 1 (count from 0).
fn (BitField) clear_bit #
fn (mut bf BitField) clear_bit(bitnr int)
clear_bit sets bit number bitnr to 0 (count from 0).
fn (BitField) extract #
fn (bf BitField) extract(start int, len int) u64
extract returns the value converted from a slice of bit numbers from start to length of len. For example 0101 . extract(1, 2) => 0b10
fn (BitField) insert #
fn (mut bf 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. For example 0000.insert(1, 2, 0b10) => 0100
fn (BitField) extract_lowest_bits_first #
fn (bf BitField) extract_lowest_bits_first(start int, len int) u64
extract_lowest_bits_first returns the value converted from a slice of bit numbers from start to length of len. For example 0101.extract_lowest_bits_first(1, 2) => 0b01
fn (BitField) insert_lowest_bits_first #
fn (mut bf BitField) insert_lowest_bits_first[T](start int, len int, _value T)
insert_lowest_bits_first sets bit numbers from start to len length with the value converted from the number _value. For example 0000.insert_lowest_bits_first(1, 2, 0b10) => 0010
fn (BitField) set_all #
fn (mut bf BitField) set_all()
set_all sets all bits in the bitfield to 1.
fn (BitField) clear_all #
fn (mut bf BitField) clear_all()
clear_all sets all bits in the bitfield to 0.
fn (BitField) toggle_bit #
fn (mut bf BitField) toggle_bit(bitnr int)
toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit number bitnr.
fn (BitField) set_if #
fn (mut bf BitField) set_if(cond bool, bitnr int)
set_if sets bit number bitnr to 1 (count from 0) if cond is true else clears the bit.
fn (BitField) toggle_bits #
fn (mut bf BitField) toggle_bits(a ...int)
toggle_bits changes the value (from 0 to 1 or from 1 to 0) of bits.
Example
mut bf := bitfield.new(10); bf.toggle_bits(1,3,5,7); assert bf.str() == '0101010100'
fn (BitField) set_bits #
fn (mut bf BitField) set_bits(a ...int)
set_bits sets multiple bits in the bitfield to 1.
Example
mut bf := bitfield.new(10); bf.set_bits(1,3,5,7); assert bf.str() == '0101010100'
fn (BitField) clear_bits #
fn (mut bf BitField) clear_bits(a ...int)
clear_bits sets multiple bits in the bitfield to 0.
Example
mut bf := bitfield.from_str('1111111111111'); bf.clear_bits(1,2,5,6,7); assert bf.str() == '1001100011111'
fn (BitField) has #
fn (bf BitField) has(a ...int) bool
has test if at least one of the bits is set.
Example
mut bf := bitfield.from_str('111111100000000'); assert bf.has(1,3,5,7)
fn (BitField) all #
fn (bf BitField) all(a ...int) bool
all test if all of the bits are set.
Example
mut bf := bitfield.from_str('111111100000000'); assert !bf.all(1,3,5,7)
fn (BitField) get_size #
fn (bf BitField) get_size() int
get_size returns the number of bits the array can hold.
fn (BitField) clone #
fn (bf 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 if they are equal.
fn (BitField) pop_count #
fn (bf BitField) pop_count() int
pop_count returns the number of set bits (ones) in the array.
fn (BitField) pos #
fn (bf BitField) pos(needle BitField) int
pos checks if the bitfield contains a sub-array needle. It returns its position if it does, -1 if it does not, and -2 on error.
fn (BitField) slice #
fn (bf BitField) slice(_start int, _end int) BitField
slice returns a sub-array of bits between _start (included) and _end (excluded).
fn (BitField) reverse #
fn (bf BitField) reverse() BitField
reverse reverses the order of bits in the bitfield (swap the first with the last, the second with the last but one and so on).
fn (BitField) resize #
fn (mut bf BitField) resize(new_size int)
resize changes the size of the bit array to new_size.
fn (BitField) rotate #
fn (bf BitField) rotate(offset int) BitField
rotate performs a circular-shift on the bits by offset positions (move offset bit to 0, offset+1 bit to 1, and so on).
fn (BitField) shift_left #
fn (bf BitField) shift_left(count int) BitField
shift_left shift-left the bits by count positions.
fn (BitField) shift_right #
fn (bf BitField) shift_right(count int) BitField
shift_right shift-right the bits by count positions.
- README
- fn bf_and
- fn bf_not
- fn bf_or
- fn bf_xor
- fn from_bytes
- fn from_bytes_lowest_bits_first
- fn from_str
- fn hamming
- fn join
- fn new
- struct BitField
- fn str
- fn free
- fn get_bit
- fn set_bit
- fn clear_bit
- fn extract
- fn insert
- fn extract_lowest_bits_first
- fn insert_lowest_bits_first
- fn set_all
- fn clear_all
- fn toggle_bit
- fn set_if
- fn toggle_bits
- fn set_bits
- fn clear_bits
- fn has
- fn all
- fn get_size
- fn clone
- fn ==
- fn pop_count
- fn pos
- fn slice
- fn reverse
- fn resize
- fn rotate
- fn shift_left
- fn shift_right