Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: [Caml-list] 32-bit unsigned integers

3 views
Skip to first unread message

Boris Yakobowski

unread,
Jul 26, 2004, 2:23:38 PM7/26/04
to Corey O'Connor, caml...@inria.fr
On Mon, Jul 26, 2004 at 10:49:25AM -0700, Corey O'Connor wrote:
> On Mon, 26 Jul 2004 19:15:18 +0200, Sébastien Hinderer
> <sebastien...@ens-lyon.org> wrote:
> > Now I'd like to define the same thing in Caml:
> >
> > # let uint32_max = Int32.of_string "4294967295";;
> > Exception: Failure "int_of_string".
>
> Ah! I asked the same question just a bit ago!
>
> A "language extension" to Ocaml (Make sure you have 3.07.11 or
> greater) is to directly assign 32 bit integers without using
> Int32.of_string. Just add an "l" to the end of the constant. (Lower
> case "L" if your font is strange and it looks like a 1 | I) It's like
> so:
>
> # let blah = 0x8FFFFFFFl;;
> val blah : int32 = -1879048193l
> #

Unfortunately, this is still not enough in Ocaml 3.08 for the unsigned
integers needed by Sebastien:

Objective Caml version 3.08

# 4294967295l;;
Integer literal exceeds the range of representable integers of type int32
#

--
Boris

-------------------
To unsubscribe, mail caml-lis...@inria.fr Archives: http://caml.inr=
ia.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

SébastienHinderer

unread,
Jul 26, 2004, 3:52:42 PM7/26/04
to caml...@inria.fr
Hi again,

Thank you very much for your help !

For sure, this new language feature is great.

However, I have another question:

With OCaml 3.08.0 :

# let max = 0xffffffffl;;
val max : int32 = -1l
# Printf.printf "%lu\n" max;;
4294967295
- : unit = ()


# 4294967295l;;
Integer literal exceeds the range of representable integers of type int32

Is this error the expected behaviour ?

Thanks in advance,
Sébastien.

SébastienHinderer

unread,
Jul 26, 2004, 4:00:10 PM7/26/04
to caml...@inria.fr
Hi again,

> 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.

Sorry, I was caught by the fact they are signed.

However, I'm not sure it is so easy. Indeed, the representation and the r=
ange
of represented integers has to be exactly the same than with the uint32_t
type of C, to allow me to use some constants defined in C in Caml program=
s.

Consider for instance the following definition in a .h C header file :

#define UINT32_MAX (4294967295U)

Now I'd like to define the same thing in Caml:

# let uint32_max = Int32.of_string "4294967295";;
Exception: Failure "int_of_string".

How can I solve this problem ?

Thanks,

Xavier Leroy

unread,
Jul 26, 2004, 4:01:20 PM7/26/04
to caml...@inria.fr
> 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-lis...@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/

0 new messages