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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!feeder.news-service.com!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Wed, 14 Jan 2009 11:08:39 +0100 From: Georg Bauhaus User-Agent: Thunderbird 2.0.0.19 (Macintosh/20081209) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com> In-Reply-To: <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Message-ID: <496db9a7$0$32672$9b4e6d93@newsspool2.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 14 Jan 2009 11:08:39 CET NNTP-Posting-Host: 260fecf9.newsspool2.arcor-online.net X-Trace: DXC=i8Sf ChristopherL schrieb: > On Jan 13, 9:33 pm, Brian Drummond > wrote: >> what are you really trying to do? >> >> - Brian > > > What I want to do is this. I have a variable holding the base 10 value > 200. It has a certain bit representation. If the variable was an > integer of 16 bits it's representation would be something such as > 0000000011001000. For give me if I'm wrong. Anyway, what I'd like to > do is put that exact bit representation in another variable of a > different type. > > How do I proceed? You use an instance of Unchecked_Conversion, as explained. Consider choosing your types (by declaring them, preferably) just like you need them. Avoid predefined types like the plague. Yes. There is an exception: there are type that are intended for directly manipulating machine words or machine registers with no other intention than to manipulate bits in ways that are neither numeric nor reflecting Boolean logic. Forgive me if I'm wrong, but you types' names sound like C's, and the approach also reminds me of what might be a reasonable way to do it in C. It is worth while giving Ada's fundamental type system a chance. It is never necessary to make assumptions about the bit size of a number. Give the range of numbers that your(!) data type needs, exactly, in a subtype constraint. If needed, request a size for this type, as explained in another post. > I was thinking of trying to do it with code such as the below. The > below code is not mine and came from this group. So, tomorrow I am > planning on modifying my first example above to use the below code. > What are my chances of doing it? > > Thanks, > Chris L. > > with TEXT_IO; > with UNCHECKED_CONVERSION; > procedure UNCHECK_DEMO is > package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER); > function INT_TO_FLT is > new UNCHECKED_CONVERSION( > SOURCE => LONG_LONG_INTEGER, > TARGET => FLOAT); > INT : LONG_LONG_INTEGER; Beware that type Long_Long_Integer is neither a standard Ada type (in that an implementation need not provide it, see RM 3.5.4 Integer Types), nor are its objects guaranteed to have the size you'd expect them to have. Use representation clauses as outlined by Ludovic if you really need tight control over bit representation. > FLT : FLOAT; Your compiler might warn you in case the two types you pass to Unchecked_Conversion do not match. See: http://en.wikibooks.org/wiki/Ada_Programming/Type_System#Unchecked_conversion > begin > INT:=2#11111111100000000000000000000000#; This value might be inacceptable all Ada compilers; the non-standard name > FLT:=INT_TO_FLT(INT); > INT_IO.put(INT,0,2); > end;