comp.lang.ada
 help / color / mirror / Atom feed
* Convert array to integer value
@ 2002-05-23 14:49 Stephen Frackelton
  2002-05-23 15:00 ` Nige
  2002-05-23 15:35 ` David Gillon
  0 siblings, 2 replies; 7+ messages in thread
From: Stephen Frackelton @ 2002-05-23 14:49 UTC (permalink / raw)


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.

is there any simple way of doing this ?

All i can think of is for loop through the array and having a translation
table for each position within the array, i.e if bit 5 = 1 then add 16 to
total, if bit 6 = 1 then add 32 to total and so on.

any other ideas ?


thanks

Stephen





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Convert array to integer value
  2002-05-23 14:49 Stephen Frackelton
@ 2002-05-23 15:00 ` Nige
  2002-05-23 15:27   ` Stephen Frackelton
  2002-05-23 15:35 ` David Gillon
  1 sibling, 1 reply; 7+ messages in thread
From: Nige @ 2002-05-23 15:00 UTC (permalink / raw)


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.
> 
> is there any simple way of doing this ?
> 
> All i can think of is for loop through the array and having a translation
> table for each position within the array, i.e if bit 5 = 1 then add 16 to
> total, if bit 6 = 1 then add 32 to total and so on.
> 
> any other ideas ?

If you make it an array of booleans, you can pack the array into 16
bits, then do an unchecked conversion into an unsigned_16 on the
array...

Also, a boolean array will allow you to do logical operations too...

Nige



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Convert array to integer value
  2002-05-23 15:00 ` Nige
@ 2002-05-23 15:27   ` Stephen Frackelton
  2002-05-23 16:36     ` Stephen Leake
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen Frackelton @ 2002-05-23 15:27 UTC (permalink / raw)


thanks, will consider this in future, but this would involve too many
changes now...

any others ?

"Nige" <nigel.scott@uk.thalesgroup.com> wrote in message
news:3CED0420.5C5A9B6D@uk.thalesgroup.com...
> 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.
> >
> > is there any simple way of doing this ?
> >
> > All i can think of is for loop through the array and having a
translation
> > table for each position within the array, i.e if bit 5 = 1 then add 16
to
> > total, if bit 6 = 1 then add 32 to total and so on.
> >
> > any other ideas ?
>
> If you make it an array of booleans, you can pack the array into 16
> bits, then do an unchecked conversion into an unsigned_16 on the
> array...
>
> Also, a boolean array will allow you to do logical operations too...
>
> Nige





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Convert array to integer value
  2002-05-23 14:49 Stephen Frackelton
  2002-05-23 15:00 ` Nige
@ 2002-05-23 15:35 ` David Gillon
  2002-05-24  8:22   ` Stuart Palin
  1 sibling, 1 reply; 7+ messages in thread
From: David Gillon @ 2002-05-23 15:35 UTC (permalink / raw)




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.
> is there any simple way of doing this ?
> All i can think of is for loop through the array and having a translation
> table for each position within the array, i.e if bit 5 = 1 then add 16 to
> total, if bit 6 = 1 then add 32 to total and so on.
> any other ideas ?

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

Sum = 0
For Count = 1 to 16 
  Sum = Sum + ((2 ** Count-1) * Integer_Array(Count))

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


David Gillon



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Convert array to integer value
  2002-05-23 15:27   ` Stephen Frackelton
@ 2002-05-23 16:36     ` Stephen Leake
  0 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2002-05-23 16:36 UTC (permalink / raw)


"Stephen Frackelton" <stephen.frackelton@baesystems.com> writes:

> thanks, will consider this in future, but this would involve too many
> changes now...
> 
> any others ?

You can pack your integer array (make sure they are unsigned
integers), and do unchecked conversion on that as well.


-- 
-- Stephe



^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Convert array to integer value
@ 2002-05-23 16:49 Beard, Frank [Contractor]
  0 siblings, 0 replies; 7+ messages in thread
From: Beard, Frank [Contractor] @ 2002-05-23 16:49 UTC (permalink / raw)


Assuming you have your 16 bit integer defined something
like the following:

with Ada.Text_Io;
with Ada.Unchecked_Conversion;

procedure BitTest is

   type Word is range -2**15 .. 2**15-1;
   for Word'size use 16;

   type Bit is mod 2;
   for Bit'size use 1;

   type Bit_Array is array (1..16) of Bit;
   pragma Pack(Bit_Array);

   function To_Integer is new
      Ada.Unchecked_Conversion(Source => Bit_Array,
                               Target => Word);

   bits   : Bit_Array := (others => 0);
   result : Word := 0;

   char   : character := ' ';

begin

   --+ Results on Windows.  Your order will
   --+ vary based on the bit order on your machine.
   --+ On Windows: High Order bit is index 16
   --+             Low  Order bit is index 1.
   --+ Running with the first two "bits" values below
   --+ will tell you immediately what you need to know.

   bits := (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1);  --+ -32768
-- bits := (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);  --+  1
-- bits := (1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0);  --+  7

   result := To_Integer(bits);

   Ada.Text_Io.New_Line;

   Ada.Text_Io.Put_Line("Word [" & Word'image(result) & " ]");

   Ada.Text_Io.New_Line;

   Ada.Text_Io.Put("Hit any key to continue ...");
   Ada.Text_Io.Get_Immediate(char);

end BitTest;


Frank


-----Original Message-----
From: Stephen Frackelton [mailto:stephen.frackelton@baesystems.com]
Sent: Thursday, May 23, 2002 10:49 AM
To: comp.lang.ada@ada.eu.org
Subject: Convert array to integer value


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.

is there any simple way of doing this ?

All i can think of is for loop through the array and having a translation
table for each position within the array, i.e if bit 5 = 1 then add 16 to
total, if bit 6 = 1 then add 32 to total and so on.

any other ideas ?


thanks

Stephen


_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Convert array to integer value
  2002-05-23 15:35 ` David Gillon
@ 2002-05-24  8:22   ` Stuart Palin
  0 siblings, 0 replies; 7+ messages in thread
From: Stuart Palin @ 2002-05-24  8:22 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-05-24  8:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-23 16:49 Convert array to integer value Beard, Frank [Contractor]
  -- strict thread matches above, loose matches on Subject: below --
2002-05-23 14:49 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox