andrenth / ocaml-stdint

Various signed and unsigned integers for OCaml
Other
85 stars 16 forks source link

ocaml-stdint

This OCaml library provides integer types having specified widths.

It provides the following unsigned integer types:

and the following list of signed integer types:

There is a module for every integer type that implements the Int interface. This interface is similar to Int32 and Int64 from the base library but provides more functions and constants like

The library also comes with C header files that allow easy access to the integer values in C bindings. The functions are modeled on base of the int32 and int64 access functions provided by the base library. The semantics of all operations is identical to the Int32, Int64 modules, C, C++, Java etc.: Conversion will silently truncate larger values as will other operations leading to overflowing integer values.

The API of stdint can be found online at the OCaml forge.

To use the integer types, we recommend to open Stdint but not the individual modules:

open Stdint

let _ =
  let a = Uint8.of_int 21 in
  let x = Uint8.(a * (one + one)) in
  print_endline (Uint8.to_string x)

The 128 bit integer types are currently only available on 64 bit platforms; the compatibility layer for 32 bit platforms is not yet fully implemented and will raise Failure for several functions.

Implementation

The representation of integers depends on their size:

Copyright

The stdint library is written by Andre Nathan, Jeff Shaw, Markus Weissmann and Florian Pichlmeier. It is based on the ocaml-uint library.

The source-code of stdint is available under the MIT license.