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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8272e612db888282 X-Google-Attributes: gid103376,public From: adam@irvine.com (Adam Beneschan) Subject: Re: -ve values in enumeration rep clauses. Date: 1996/06/14 Message-ID: <4ps65i$v1g@krusty.irvine.com>#1/1 X-Deja-AN: 160190716 distribution: world references: <5c3p6DALoKwxEwb0@djcull.demon.co.uk> organization: /z/news/newsctl/organization newsgroups: comp.lang.ada Date: 1996-06-14T00:00:00+00:00 List-Id: Darel Cullen writes: >Hi, > Came across an interesting problem at work today. > >What happens if for example :- > >type A_Type is > ( A, B, C ); > >for A_Type use > ( > A => -1, > B => -2, > C => -3 > ); > >for A_Type'Size use 8; > >Now - to be able to extract the -ve values via unchecked_conversion >from a byte proves impossible because the use 8 creates an unsigned >byte, so whats the best way to represent negative values from a >rep-claused enumerated type ? > I'm not following any of this. First, the first rep clause for A_Type is illegal, because if you have an enumeration rep clause, the integers have to be in ascending order (assuming the enumeration literals are listed in order). The integers you have here are in descending order. Second, I don't see how "use 8" creates an unsigned byte. It simply allocates 8 bits for each object of type A_Type. From an Ada standpoint, you can't call it "signed" or "unsigned", since the values represented in those 8 bits are enumeration values and not integers. If what you mean to say is that the compiler is generating unsigned-byte instructions to deal A_Type, then your compiler probably has a bug. Even so, that shouldn't affect things too much if all the integers in the rep clause are negative. It would be worse if you said for A_Type use ( A => -1, B => 0, C => 1 ); and now, when you compare to A_Types, the generated code might erroneously think A > C if it treates the bytes as unsigned during the comparison. I'm not sure how unchecked_conversion would be affected, though. If you say type Signed_Byte is integer range -128 .. 127; for Signed_Byte'size use 8; function To_Signed_Byte is new Unchecked_Conversion (A_Type, Signed_Byte); then when you convert an A_Type to a Signed_Byte, the compiler should simply be copying the bit pattern, so it doesn't matter whether the compiler thinks of A_Type as "signed" or "unsigned". The Unchecked_Conversion will make it signed. If you're trying something like this: type Signed_Word is integer range -(2**31) .. 2**31-1; for Signed_Word'size use 32; function To_Signed_Word is new Unchecked_Conversion (A_Type, Signed_Word); I don't know what results you will get, and I don't recommend doing this in any case. I don't like using Unchecked_Conversion when the two types don't have the same size. In any case, if you want help with your problem, it would be best to show us exactly what Ada code you were trying to run and what unexpected results you got. Your description doesn't make it clear just what you were trying to do. -- Adam