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
Re: Unsigned integers?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-03-24 (15:31)
From: Damien Doligez <Damien.Doligez@i...>
Subject: Re: Unsigned integers?
>From: Max Skaller <>

>I would be happy to replace, in this code,
>evey use of 'lor', 'land', + - * < etc with
>'ulor' 'uland' 'uplus' 'uminus' 'uless' etc, if only
>I could define them. (I could do this in C .. but then,
>I could write the below routines in C too)

For ulor, uland, uplus, uminus, umult, as well as lsr and lsl, they
are identical to their signed counterparts, so you don't need to do

For uless, since you are only ever comparing to a positive constant
less than max_int, I suggest replacing "if i < constant" with
"if 0 <= i && i < constant".

>Note these operations MUST be extremely fast,

If the above works, I doubt you can go any faster.  For more complex
code, you may have to use a full-blown unsigned comparison:
(not tested; could be wrong)

  let uless x y = if (x < 0) = (y < 0) then x < y else x > y;;

The only difficulty would be with division and modulo, as noted by
Xavier, but I gather you don't need them for this application.

-- Damien