From: "Beard, Frank [Contractor]" <beardf@spawar.navy.mil>
Subject: RE: Convert array to integer value
Date: Thu, 23 May 2002 12:49:46 -0400
Date: 2002-05-23T12:49:46-04:00 [thread overview]
Message-ID: <mailman.1022172663.15144.comp.lang.ada@ada.eu.org> (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
next reply other threads:[~2002-05-23 16:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-05-23 16:49 Beard, Frank [Contractor] [this message]
-- strict thread matches above, loose matches on Subject: below --
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox