Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] 32-bit unsigned integers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-07-26 (16:46)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] 32-bit unsigned integers
> Does a type representing unsigned 32-bits integer exist in Caml ?

Yes: it's called int32.  Think about it: being "unsigned" or "signed"
is not a property of the representation (it will be 32 binary digits
in both cases), it's just that some operations (division, modulus and
comparisons) interpret those bits differently.

> All I'd need is an abstract type defining these integers, plus some
> functions to go from strings to 32-bits unsigned integers and vice-versa.

Here you are:

module UInt32 = struct
  type t = int32
  let of_string = Int32.of_string
  let to_string n = Printf.sprintf "%lu" n

You can throw in some arithmetic operations as well:

  let add = Int32.add
  let sub = Int32.sub
  let mul = Int32.mul

etc.  As I said above, the only operations that need to be treated
specially are comparisons and division/modulus.

Hope this helps,

- Xavier Leroy

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: