comp.lang.ada
 help / color / mirror / Atom feed
* 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
* 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

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