comp.lang.ada
 help / color / mirror / Atom feed
From: Stuart Palin <stuart.palin@0.0>
Subject: Re: Convert array to integer value
Date: Fri, 24 May 2002 09:22:09 +0100
Date: 2002-05-24T09:22:09+01:00	[thread overview]
Message-ID: <3CEDF831.442A8CFB@0.0> (raw)
In-Reply-To: 3CED0C3F.637A52D8@spamtrap.baesystems.com

David Gillon wrote:
> 
> Stephen Frackelton wrote:
> > I have set up an array of integers, value 0..1, to simulate a
> > word, 16 bits.  I need to convert this array of 0's and 1's
> > from the binary value to its corresponding integer value.

> Without getting into questions over why you would want to use a
> datastructure like that, try the following algorithm:

Agreed.

The Stephen Leake suggestion is possibly the best if you
have control over the declaration of the array, and the
array is not being used for other purposes which would make
it desirable to access each element as a simple entity
(issues of execution performance may become relevant in that
case).

I think David's suggestion can be tidied a bit; I would
suggest an algorithm such as

  Sum := 0;
  for i in reverse 1..15 loop
    Sum := Sum*2 + My_Array(i);
  end loop;
  if (My_Array(16) = 1) then
    Sum := Sum - 65536;
  end if;

[ If your compiler is too dumb to optimize the *2 to either
a shift or an add then re-write it yourself as Sum+Sum].


> (And there's also a perfectly good internal company newsgroup for Ada
> questions)
Seconded!

--
Stuart Palin
Principal Software Engineer
BAE SYSTEMS Avionics Ltd, Rochester



  reply	other threads:[~2002-05-24  8:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-23 14:49 Convert array to integer value Stephen Frackelton
2002-05-23 15:00 ` Nige
2002-05-23 15:27   ` Stephen Frackelton
2002-05-23 16:36     ` Stephen Leake
2002-05-23 15:35 ` David Gillon
2002-05-24  8:22   ` Stuart Palin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-05-23 16:49 Beard, Frank [Contractor]
replies disabled

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