Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in
news:1110082147 .830198@athnrd0 2:
Jim Rogers wrote:
The program declares a string S initialized to "This is a text
message". It then declares an array type named Bits_Array. That array
type is an array of Boolean, with the number of elements equal to the
number of bits in used by S. The variable L is set to equal the value
returned by the Size attribute of the String S. Ada reports size as
the number of bits, not the number of bytes.
A packed array of boolean allocates one storage element to each bit.
Therefore, the statement
pragma Pack(Bits_Array );
causes all instances of Bits_Array to be a packed array.
The variable Bits is declared to be of the type Bits_Array.
The next statement forces Bits to overlay S. Both variables
are the same size and start at the same address.
Does this mean that Boolean always occupies 1 bit and has no padding
bits?
No. A boolean usually occupies a byte. A packed array of boolean
is compressed so that each boolean element of the array occupies only
one bit.
The body of the procedure simply iterates through all the bits in
the Bits variable and prints the numeric value of each bit.
A new line is output whenever the loop control variable mod
System.Storage_ Unit evaluates to 0. System.Storage_ Unit is
provided by Ada so that the program will properly represent
each storage unit (sometimes called a byte) no matter what the
size of that unit is.
As you can clearly see, Ada can represent the bits of a
variable with very little difficulty.
Ada does not store a null at the end of a string. This is
why there is no indication of a null value for the last byte.
While this is an interesting thing, I have the feeling that this
approach does not print all bits, including padding bits, of a
*user-defined type*.
The same approach will work for any type, including any padding
bits. The Size attribute reports the total number of bits used
by an object.
In C++ you can read (and thus copy, print or anything) every byte of
any type.
In the example you provided, I have the feeling that you allocated a
character array (the string) and then treated is a boolean array
(somewhat a hacking attempt to imitate the behaviour).
I created a string, which is an array of character in Ada.
I also created an packed array of boolean with exactly the same
size as the array of character. I then specified that both
variables will occupy the same space; one overlays the other.
However what happens in the case of a user defined type (I suppose Ada
supports OO programming) or a record. Can you print the byte
implementation of such an object?
Also for a built in type, say a floating point, can you print its
implementation bytes too (including padding bits)?
The following example starts with the creation of a generic package
for printing the byte and bit output of an object of any type.
The program then instantiates that package for a user-defined
type and for a long_float, which is equivalent to a C++ double.
generic
type Target_Type is private;
Target_Size : Natural;
package Bit_Utils is
procedure Show_Bits(Item : Target_Type);
end Bit_Utils;
with Ada.Text_Io;
with Ada.Integer_Tex t_Io;
with System;
package body Bit_Utils is
type Bits_Array is array(Positive range <>) of Boolean;
pragma Pack(Bits_Array );
type Byte is mod 2**8;
type Byte_Array is array(Positive range <>) of Byte;
package Mod_Io is new Ada.Text_IO.Mod ular_IO(Byte);
procedure Show_Bits(Item : Target_Type) is
Bit_View : Bits_Array(1..T arget_Size);
for Bit_View'Addres s use Item'Address;
Byte_View : Byte_Array(1..T arget_Size / Byte'Size);
For Byte_View'Addre ss use Item'Address;
begin
for I in Byte_View'range loop
Mod_Io.Put(Item => Byte_View(I), Width => 4);
end loop;
Ada.Text_IO.New _Line(2);
for I in Bit_View'range loop
Ada.Integer_Tex t_Io.Put(Item => Boolean'Pos(Bit _View(I)),
Width => 1);
if I mod System.Storage_ Unit = 0 then
Ada.Text_IO.New _Line;
end if;
end loop;
end Show_Bits;
end Bit_Utils;
with Bit_Utils;
with Ada.Text_IO;
procedure Bit_Output is
type My_Type is record
Name : String(1..4);
Age : Positive;
Weight : Long_Float;
end record;
package My_Bits is new Bit_Utils(My_Ty pe, My_Type'Size);
package Flt_Bits is new Bit_Utils(Long_ Float, Long_Float'Size );
Mt : My_Type := ("Jim ", 55, 0.45435);
D : Long_Float := 0.45435;
begin
Ada.Text_Io.Put _Line("Output of My_Type");
My_Bits.Show_Bi ts(Mt);
Ada.Text_Io.Put _Line("Output of Long_Float");
Flt_Bits.Show_B its(D);
end Bit_Output;
The output of this program is:
Output of My_Type
74 105 109 32 55 0 0 0 163 1 188 5 18 20 221 63
01010010
10010110
10110110
00000100
11101100
00000000
00000000
00000000
11000101
10000000
00111101
10100000
01001000
00101000
10111011
11111100
Output of Long_Float
163 1 188 5 18 20 221 63
11000101
10000000
00111101
10100000
01001000
00101000
10111011
11111100
Ada provides capabilities similar to C++ in the area of copying
and viewing data.
Jim Rogers