10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-06-02 03:15:19 +02: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

<<<~Bitstring.t~>>>

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

Example:

Bitstring.of_int 15;;
- : Bitstring.t =
++++------------------------------------------------------------

Example:

Bitstring.(shift_left (of_int 15) 2);;
- : Bitstring.t =
--++++----------------------------------------------------------

Bitstring.(shift_right (of_int 15) 2);;
- : Bitstring.t =
++--------------------------------------------------------------

Bitstring.shift_left_one 32 4;;
- : Bitstring.t =
----+-----------------------------------------------------------

Bitstring.(testbit (of_int 15) 3);;
- : bool = true

Bitstring.(testbit (of_int 15) 4);;
- : bool = false

Example:

Bitstring.(logor (of_int 15) (of_int 73));;
- : Bitstring.t =
++++--+---------------------------------------------------------

Bitstring.(logand (of_int 15) (of_int 10));;
- : Bitstring.t =
-+-+------------------------------------------------------------

Bitstring.(logxor (of_int 15) (of_int 73));;
- : Bitstring.t =
-++---+---------------------------------------------------------

Example:

Bitstring.(plus_one (of_int 15));;
- : Bitstring.t =
----+-----------------------------------------------------------

Bitstring.(minus_one (of_int 15));;
- : Bitstring.t =
-+++------------------------------------------------------------

Example:

Bitstring.(trailing_zeros (of_int 12));;
- : int = 2

Bitstring.(hamdist (of_int 15) (of_int 73));;
- : int = 3

Bitstring.(popcount (of_int 15));;
- : int = 4

Example:

Bitstring.(to_list (of_int 45));;
- : int list = [1; 3; 4; 6]

Example:

Bitstring.permutations 2 4;;
- : Bitstring.t list =
[++--------------------------------------------------------------;
+-+-------------------------------------------------------------;
-++-------------------------------------------------------------;
+--+------------------------------------------------------------;
-+-+------------------------------------------------------------;
--++------------------------------------------------------------]

Printers

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