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,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!feed.news.tiscali.de!newsfeed.tiscali.ch!stargate.gts.cz!newsfeed2.ip.tiscali.net!transit0.news.tiscali.nl!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: shifting bits References: From: Ludovic Brenta Date: Sun, 29 Aug 2004 15:21:09 +0200 Message-ID: <87d61a5bcq.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:WrLwPPAKATo7XrSLCcFUaOXCT2I= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 29 Aug 2004 15:22:29 CEST NNTP-Posting-Host: 83.134.240.220 X-Trace: 1093785749 dreader2.news.tiscali.nl 62360 83.134.240.220:34516 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:3135 Date: 2004-08-29T15:22:29+02:00 List-Id: Fabio de Francesco writes: > Talking about C/C++ you may want to shift a signed numeric variable > one position by one to the right in order to either inspect or print > each single bit that composes the number. In Ada, you would to an Unchecked_Conversion to an array of bits, and access each bit by position number. You can do that from any type. > In low-level code, If you know what you are shifting and why, you > won't have any bad result. May be that I'm used to Assembly where > you often do shifting with every type of value for the most > unthinkable reason. In assembler, you can only do bit-shifting on data types that have a "natural" size for your hardware (e.g. 8, 16, 32 and 64 bits). Ada allows you to have types with arbitrary sizes, in particular when embedding them into records, as in my earlier example where the Month is on 4 bits, Day on 5 bits and Year on 11 bits. This is why, in Ada, the bit-shifting operations are only defined (in package Interfaces) for certain sizes on modular types, corresponding to the target hardware's "natural" sizes. -- Ludovic Brenta.