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.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, STOX_REPLY_TYPE autolearn=no 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!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe21.iad.POSTED!92f42029!not-for-mail From: "Steve D" Newsgroups: comp.lang.ada References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <7b017de2-951a-414a-8290-111353fe02f8@r15g2000prd.googlegroups.com> <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com> <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com> <9fa07f9b-90bf-4a28-bf8e-09c2152bd43f@o40g2000prn.googlegroups.com> <9b117764-7b49-4b76-98b2-f7fee00c8c76@z27g2000prd.googlegroups.com> <496f726e$0$30222$9b4e6d93@newsspool1.arcor-online.net> <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com> In-Reply-To: <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com> Subject: Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Windows Mail 6.0.6001.18000 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18049 Message-ID: X-Complaints-To: abuse@teranews.com NNTP-Posting-Date: Fri, 16 Jan 2009 03:17:04 UTC Organization: TeraNews.com Date: Thu, 15 Jan 2009 19:17:00 -0800 Xref: g2news2.google.com comp.lang.ada:4341 Date: 2009-01-15T19:17:00-08:00 List-Id: "ChristopherL" wrote in message news:97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com... > If it is not possible to do what I want. > > Can someone modify the below program to show me how to set a high > order bit of a > 10 number number, and also maintain all other bits. > > procedure test2 is > > subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1); > > arg:float; > Result:Short_integer1; > > begin > > arg := 200.0 > > if (arg > 128.0) then > > arg := arg - 128.0; > --Result := ?; > > else > > Result := Short_integer1(arg); > > end if; > > end test2; > > In this example I would like Result to have a bit representation such > as: > > 10 0100 1000 > > If you have a simplier algorithm (implemented) please share it with > me. > > Thanks, > Chris L. > > See if this does what you want: with Interfaces; use Interfaces; with Ada.Unchecked_Conversion; with Ada.Text_IO; with Ada.Float_Text_IO; procedure TestSteve is subtype Short_Integer is Integer range -128..127; package Short_Io is new Ada.Text_Io.Integer_IO( Short_Integer ); package Unsigned_8_Io is new Ada.Text_Io.Modular_IO( Unsigned_8 ); function Float_To_Short_Integer( value : float ) return Short_Integer is function Conv is new Ada.Unchecked_Conversion( Unsigned_8, Short_Integer ); begin return Conv( Unsigned_8( value ) ); end Float_To_Short_Integer; procedure TestValue( value : float ) is result : Short_Integer; function Conv is new Ada.Unchecked_Conversion( Short_Integer, Unsigned_8 ); begin result := Float_To_Short_Integer( value ); Ada.Float_Text_IO.Put( value, 3, 1, 0 ); Ada.Text_IO.Put( " => "); Short_IO.Put( Float_To_Short_Integer( value ) ); Ada.Text_IO.Put( " => "); Unsigned_8_IO.Put( Conv( Float_To_Short_Integer( value ) ), 4, 2 ); Ada.Text_IO.New_Line; end TestValue; begin TestValue( 0.0 ); TestValue( 127.0); TestValue( 200.5 ); end TestSteve; Output: 0.0 => 0 => 2#0# 127.0 => 127 => 2#1111111# 200.5 => -55 => 2#11001001# Regards, Steve