comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Re: Help with type conversion needed
Date: 1996/01/08
Date: 1996-01-08T00:00:00+00:00	[thread overview]
Message-ID: <DKv8In.865.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 4cnivs$m1k@flood.weeg.uiowa.edu

Nate Bohlmann (njb@res.eng.uiowa.edu) wrote:
: Tucker Taft (stt@henning.camb.inmet.com) wrote:

: : Unchecked conversion is the reliable way to convert between
: : a 16-bit signed and 16-bit unsigned value.  This is entirely
: : portable, and safe so long as both signed and unsigned subtypes
: : are unconstrained (i.e. cover the full range of values).

: I'm not sure what you mean here..  Obviously unchecked conversion would
: preserve the bit patterns but that's not where I'm having problems.

: Say I have some types as follows:

: type SignedInt is range -32768..32767;
: for SignedInt'Size use 16;

: type UnsignedInt is mod 65536;
: for UnsignedInt'Size use 16;


: Assume the SignedInt is coming off an A/D converter and I want to use
: unsigned values in my calculations and I can't convince the A/D converter
: to return unsigned values.  I believe that in order to keep relative 
: values correct, I would need to add 32768 to the SignedInt variable to 
: get a proper value for the UnsignedInt variable.  

"... to keep relative values correct ..."
This seems to be a critical requirement, which I didn't understand before.
I presumed you wanted to simply interpret the bits as an unsigned number.

In any case, if you have 32-bit ints, then your problem is easy.
Just convert to a 32-bit signed int, add 32768, and convert to
the 16-bit unsigned type.  

However, if you can't do that, then use unchecked conversion to convert 
to the unsigned type, and then add (or subtract) 32768 using unsigned 
modular arithmetic, which wraps around.

: ... How do I go about doing
: this properly and portably?  The added twist of course is that the SignedInt
: is in little-endian format and I'm working on a big-endian machine.  I've 
: solved the twist  ...

This is presumably done with unchecked conversion, or multiply
and add.  If so, then just convert directly to the unsigned modular 
type, and then add 32768.  

There is no need to ever go through a signed type; that is what
is creating your problems.  Use unsigned types exclusively if you
need to do this kind of biasing or bit/byte-level fiddling.
You won't get any overflows.  As soon as you use a signed type,
you can run into overflow problems when trying to do biasing, etc.

: Thanks again..

: Nathan Bohlmann                           preferred->   njb@res.eng.uiowa.edu

-Tucker Taft  stt@inmet.com




       reply	other threads:[~1996-01-08  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4ckmb7$vo0@flood.weeg.uiowa.edu>
     [not found] ` <DKrJpC.9HE.0.-s@inmet.camb.inmet.com>
     [not found]   ` <4cnivs$m1k@flood.weeg.uiowa.edu>
1996-01-08  0:00     ` Tucker Taft [this message]
     [not found]     ` <dewar.821029372@schonberg>
1996-01-08  0:00       ` Help with type conversion needed Nate Bohlmann
1996-01-08  0:00         ` Robert Dewar
1996-01-08  0:00           ` Keith Thompson
1996-01-09  0:00         ` Geert Bosch
     [not found]           ` <dewar.821197198@schonberg>
1996-01-09  0:00             ` Nate Bohlmann
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox