Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
end

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 caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners