From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada,comp.lang.c++ Subject: Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada) From: Jim Rogers References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <87is4598pm.fsf@insalien.org> <1110054476.533590@athnrd02> <1110059861.560004@athnrd02> <1110072229.85604@athnrd02> <1110082147.830198@athnrd02> User-Agent: Xnews/5.04.25 Message-ID: Date: Sun, 06 Mar 2005 06:12:02 GMT NNTP-Posting-Host: 12.73.181.153 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1110089522 12.73.181.153 (Sun, 06 Mar 2005 06:12:02 GMT) NNTP-Posting-Date: Sun, 06 Mar 2005 06:12:02 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:8723 comp.lang.c++:44289 Date: 2005-03-06T06:12:02+00:00 List-Id: Ioannis Vranos wrote in news:1110082147.830198@athnrd02: > 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_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