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

Hope this helps.



To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: