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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2a0aa2b1c348fd6a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.stueberl.de!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: shifting bits Date: Sun, 29 Aug 2004 08:51:55 +0200 Organization: AdaCL Message-ID: <6476129.Z2ZE0Tfv6r@linux1.krischik.com> References: Reply-To: krischik@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1093768076 06 2458 zgEDXQaOVDMcOVi 040829 08:27:56 X-Complaints-To: usenet-abuse@t-online.de X-ID: JrDGWeZdgeuHGc41i6HbuQlI5976DTVWvb9KBeoZ7FFIpJ8HWQPTgE User-Agent: KNode/0.7.7 Xref: g2news1.google.com comp.lang.ada:3131 Date: 2004-08-29T08:51:55+02:00 List-Id: Jim Rogers wrote: > fmdf@tiscali.it (fabio de francesco) wrote in > news:ba2f9c57.0408281738.61a48643@posting.google.com: > >> Hello. >> >> I had to pack/unpack some variables into/from one. In order to get the >> result I chose to shift the variables' bits using multiplications and >> divisions by powers of two. >> >> 1) Does Ada have a built-in shift operand like C++ "<<" and ">>" ? > > Ada provides similar bit shifting operations in the package Interfaces. > Those operations are defined for specific instances of modular types. > >> 2) Can only variables of modular type be shifted ? If yes, why ? > > Even in C and C++ bit shifting is only correct for unsigned types. > Think about what would happen if you shift a sign bit. The > resulting values would be really nasty depending upon the state of > the sign bit. And wen you still need the potentional dangerous operation you could address map an unsigned type: I : Interfaces.Integer_32; U : Interfaces.Unsigned_32; for U'Address use I'Address; begin I := 2* 30; Interfaces.Shift_Left (U, 1); And I will have just the same wacky result als in C. > Modular types are Ada's unsigned types. To clarify: Range types alwas have sign - even when the range itself does not allow a negative value. With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com