J
Jim Rogers
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.
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_Text_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.Modular_IO(Byte);
procedure Show_Bits(Item : Target_Type) is
Bit_View : Bits_Array(1..Target_Size);
for Bit_View'Address use Item'Address;
Byte_View : Byte_Array(1..Target_Size / Byte'Size);
For Byte_View'Address 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_Text_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_Type, 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_Bits(Mt);
Ada.Text_Io.Put_Line("Output of Long_Float");
Flt_Bits.Show_Bits(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