15 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
of_int
Creates a bit string from an int
.
val of_int : int -> t
of_z
Creates a bit string from an Z.t
multi-precision integer.
val of_z : Z.t -> t
zero
zero n
creates a zero bit string with n
bits.
val zero : int -> t
numbits
Returns the number of bits used to represent the bit string.
val numbits : t -> int
is_zero
True if all the bits of the bit string are zero.
val is_zero : t -> bool
neg
Returns the negative of the integer interpretation of the bit string.
neg (of_int x) = neg (of_int (-x))
val neg : t -> t
shift_left
shift_left t n
returns a new bit strings with all the bits
shifted n
positions to the left.
val shift_left : t -> int -> t
shift_right
shift_right t n
returns a new bit strings with all the bits
shifted n
positions to the right.
val shift_right : t -> int -> t
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.
val shift_left_one : int -> int -> t
testbit
testbit t n
is true if the n
-th bit of the bit string t
is
set to 1
.
val testbit : t -> int -> bool
logor
Bitwise logical or.
val logor : t -> t -> t
logxor
Bitwise logical exclusive or.
val logxor : t -> t -> t
logand
Bitwise logical and.
val logand : t -> t -> t
lognot
Bitwise logical negation.
val lognot : t -> t
minus_one
Takes the integer representation of the bit string and removes one.
minus_one (of_int 10) = of_int 9
val minus_one : t -> t
plus_one
Takes the integer representation of the bit string and adds one.
plus_one (of_int 10) = of_int 11
val plus_one : t -> t
trailing_zeros
Returns the number of trailing zeros in the bit string.
val trailing_zeros : t -> int
hamdist
Returns the Hamming distance, i.e. the number of bits differing between two bit strings.
val hamdist : t -> t -> int
popcount
Returns the number of bits set to one in the bit string.
val popcount : t -> int
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
.
Bitstring.to_list (of_int 5);; - : int list = [1; 3]
val to_list : ?accu:(int list) -> t -> int list
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.
Bitstring.permutations 2 4 |> List.map (fun x -> Format.asprintf "%a" Bitstring.pp x) ;; - : string list = ["++--------------------------------------------------------------"; "+-+-------------------------------------------------------------"; "-++-------------------------------------------------------------"; "+--+------------------------------------------------------------"; "-+-+------------------------------------------------------------"; "--++------------------------------------------------------------"]
val permutations : int -> int -> t list
Printers
val pp : Format.formatter -> t -> unit