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