English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Need unsigned int
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-01-24 (00:39)
From: David Chase <chase@w...>
Subject: Re: [Caml-list] Need unsigned int
My CAML skills are poor, especially on bitwise operations, but
here is how one does (or can do) unsigned in Java:

  public static boolean lessThan(int x, int y) {
    return ((x^y) >= 0) ? (x < y) : (y < x);
  };

That is, if the signs are the same, then signed and
unsigned compares agree.  Otherwise, the negative one
is larger.  Another way to phrase this might be (if
you hate control flow):

  return ((x^y) >>> 31) ^ (x < y);

The reasoning here is that if the signs agree, then
0 XOR any == any.  Otherwise, if the signs disagree,
then x and y are surely not equal, so (y<x) and
(y<=x) serve equally well, but (y<=x) == !(x<y),
which has a subexpression in common with the other
case.

Hope this helps.

David


 

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