caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Eric Cooper <ecc@cmu.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] ocaml, int32/64, bigarray and unsigned values ...
Date: Mon, 11 Apr 2005 12:13:44 -0400	[thread overview]
Message-ID: <20050411161344.GB17332@localhost> (raw)
In-Reply-To: <20050411153551.GA10362@pegasos>

On Mon, Apr 11, 2005 at 05:35:51PM +0200, Sven Luther wrote:
> Now, what about conversion to Int32 or Int64 ? Would an unsigned
> Int32 which is represented as a negative signed Int32 not get broken
> when used to calculate Int64 values ?

You'll have to watch out for sign-extension: when a signed integer is
widened, the leading bits get filled with 1s to preserve the sign.
That's the wrong behavior if you want to widen an unsigned integer.
The Int{32,64} modules don't seem to have of_unsigned_int functions,
but you can simulate them by checking if the result is negative and
adjusting it (by adding 2^n).

> And what about comparisons ?

Right, you'll have to define your own, because for example -1 < 0,
but you want 0 < 0xFF...FF.  You can just test for negative numbers to
simulate it yourself (since any negative int is greater than any
positive int when treating them as unsigned, otherwise the native int
comparison works).

> Obviously max_int + 1 > max_int will be wrong since max_int + 1
> would be considered a negative number (-0 maybe ?).

Well, max_int + 1 = min_int, but that's what you want when that bit pattern is
interpreted as unsigned.  The only incorrect results will come from
overflow, which silently "wraps around" just like in C.

-- 
Eric Cooper             e c c @ c m u . e d u


  reply	other threads:[~2005-04-11 16:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-11  7:46 Sven Luther
2005-04-11 12:57 ` [Caml-list] " Eric Cooper
2005-04-11 15:35   ` Sven Luther
2005-04-11 16:13     ` Eric Cooper [this message]
2005-04-13  6:54     ` Florian Hars
2005-04-13 18:28     ` Ken Rose
2005-05-25  6:06     ` partition tables and ocaml Taras
2005-05-25  9:09       ` Sven Luther
2005-04-12 17:19   ` [Caml-list] ocaml, int32/64, bigarray and unsigned values Paul Snively

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050411161344.GB17332@localhost \
    --to=ecc@cmu.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).