From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FROM_NUMERIC_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,95db39b88fd27b40 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-24 01:22:32 PST Message-ID: <3CEDF831.442A8CFB@0.0> Date: Fri, 24 May 2002 09:22:09 +0100 From: Stuart Palin Organization: BAE SYSTEMS Avionics X-Mailer: Mozilla 4.5 [en] (WinNT; I) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Convert array to integer value References: <3ced014d$1@pull.gecm.com> <3CED0C3F.637A52D8@spamtrap.baesystems.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: rc2966.rochstr.gmav.gecm.com X-Trace: pull.gecm.com 1022228532 rc2966.rochstr.gmav.gecm.com (24 May 2002 09:22:12 +0100) Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!news.gtei.net!newsfeed.mathworks.com!btnet-peer0!btnet-feed5!btnet!newreader.ukcore.bt.net!pull.gecm.com!not-for-mail Xref: archiver1.google.com comp.lang.ada:24629 Date: 2002-05-24T09:22:09+01:00 List-Id: 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