10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-23 04:43:32 +01:00
QCaml/common/bitstring.org

16 KiB

Bit string

We define here a data type to handle bit strings efficiently. When the bit string contains less than 64 bits, it is stored internally in a 63-bit integer and uses bitwise instructions. When more than 63 bits are required, the zarith library is used to consider the bit string as a multi-precision integer.

Type

type t

General implementation

val of_int : int -> t
val of_z   : Z.t -> t
val zero   : int -> t

val is_zero : t -> bool
val numbits : t -> int
val testbit : t -> int -> bool

val neg            : t -> t
val shift_left     : t -> int -> t
val shift_right    : t -> int -> t
val shift_left_one : int -> int -> t

val logor  : t -> t -> t
val logxor : t -> t -> t
val logand : t -> t -> t
val lognot : t -> t

val plus_one  : t -> t
val minus_one : t -> t

val hamdist        : t -> t -> int
val trailing_zeros : t -> int
val popcount       : t -> int

val to_list      : ?accu:(int list) -> t -> int list
val permutations : int -> int -> t list
of_int Creates a bit string from an int
of_z Creates a bit string from an Z.t multi-precision integer
zero zero n creates a zero bit string with n bits
is_zero True if all the bits of the bit string are zero.
numbits Returns the number of bits used to represent the bit string
testbit testbit t n is true if the n-th bit of the bit string t is set to 1
neg Returns the negative of the integer interpretation of the bit string
shift_left shift_left t n returns a new bit strings with all the bits shifted n positions to the left
shift_right shift_right t n returns a new bit strings with all the bits shifted n positions to the right
shift_left_one shift_left_one size n returns a new bit strings with the n-th bit set to one. It is equivalent as shifting 1 by n bits to the left, size is the total number of bits of the bit string
logor Bitwise logical or
logxor Bitwise logical exclusive or
logand Bitwise logical and
lognot Bitwise logical negation
plus_one Takes the integer representation of the bit string and adds one
minus_one Takes the integer representation of the bit string and removes one
hamdist Returns the Hamming distance, i.e. the number of bits differing between two bit strings
trailing_zeros Returns the number of trailing zeros in the bit string
permutations permutations m n generates the list of all possible n-bit strings with m bits set to 1. Algorithm adapted from Bit twiddling hacks
popcount Returns the number of bits set to one in the bit string
to_list Converts a bit string into a list of integers indicating the positions where the bits are set to 1. The first value for the position is not 0 but 1
minus_one (of_int 10) = of_int 9
plus_one (of_int 10) = of_int 11
Bitstring.to_list (of_int 5);;
- : int list = [1; 3]
Bitstring.permutations 2 4
|> List.map (fun x -> Format.asprintf "%a" Bitstring.pp x) ;;
- : string list =
["++--------------------------------------------------------------";
"+-+-------------------------------------------------------------";
"-++-------------------------------------------------------------";
"+--+------------------------------------------------------------";
"-+-+------------------------------------------------------------";
"--++------------------------------------------------------------"]

Printers

val pp : Format.formatter -> t -> unit