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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!pasteur!ucbvax!telesoft.UUCP!garym From: garym@telesoft.UUCP (Gary Morris @flash) Newsgroups: comp.lang.ada Subject: Re: unsigned integers in Ada Message-ID: <8804270839.AA00686@ucsd.edu> Date: 27 Apr 88 07:30:18 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet List-Id: > From: ncar!noao!mcdsun!mcdchg!clyde!rds%moss.ATT.COM@AMES.ARC.NASA.GOV > Subject: unsigned integers in Ada > > How does one implement an unsigned integer in Ada and have a maximum > value equal to 2**(number of bits) in any given implementation? The LRM requires that predefined integer types be symmetrical around zero (LRM 3.5.4 (7)). It is illegal to provide an unsigned integer type in package standard. So the largest type provided on a 32 bit machine will normally be -(2**31)..(2**31)-1. That means that you can't declare a type that is range 0..(2**32)-1 unless the compiler supports a standard integer type large enough to contain that range (and it must be symmetrical around zero)! The following declaration would require 33 bit or larger (ie: 64 bit) integer defined in package standard in order for the compiler to accept it: type Unsigned_Long is range 0..(2**32)-1; -- 64 bit base type for Unsigned_Long'size use 32; -- packed in 32 bits The expressions using the Unsigned_Long would have to be evaluated using 64-bit arithmetic for intermediate results. Due to these restrictions, unsigned types in Ada are just a form of packing. The compiler is not allowed to perform unsigned operations on the type as you would in assembly making operations on such types less efficient. It would be interesting to hear why the language designers chose to restrict types this way. The following declaration is legal on most compilers because most support a integer type in package standard that is 32 bits. The operations are still done using the 32 bit base type that contains the positive range you want but must also be symmetrical around zero: type Unsigned_Word is range 0..(2**16)-1; -- 32 bit base type for Unsigned_Word'size use 16; -- packed in 16 bits > subtype Pointer is INTEGER range 0..(2**32)-1; > for Pointer'SIZE use 32; > > is illegal because it is not a LRM 13.1 (3) > WHAT IS A FIRST NAMED SUBTYPE?? And can someone give an example? You can't change the representation of a subtype unless it is a first-named subtype since subtypes derive their representation from the base type. A is a subtype declared by a type declaration where the base type is anonymous. For example: type T is range L .. R; -- example from LRM 3.5.4 (5) --GaryM P.S. Thanks go to JPD for explaining this issue. Gary Morris UUCP: ucbvax!ucsd!telesoft!garym TeleSoft, San Diego telesoft!garym@ucsd.edu (619) 457-2700 ARPA: telesoft!garym@ucsd.ARPA