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!newsfeed1.ip.tiscali.net!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 13:29:44 +0200 Message-ID: <87eklqfahj.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:r8Q8yhrkIA+MdLWLZfcOfdJMJFs= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 29 Aug 2004 13:31:03 CEST NNTP-Posting-Host: 83.134.240.220 X-Trace: 1093779063 dreader2.news.tiscali.nl 62395 83.134.240.220:34377 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:3133 Date: 2004-08-29T13:31:03+02:00 List-Id: Fabio de Francesco writes: > 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. Others have responded to the modular type part of your question, but I suspect that modular types and shifting is not the best way to solve your problem. In Ada, you can express your problem much more cleanly, using representation clauses: procedure Packed_Date is type Day_Type is range 1 .. 31; for Day_Type'Size use 5; type Month_Type is range 1 .. 12; for Month_Type'Size use 4; type Year_Type is range 1900 .. 2099; for Year_Type'Size use 11; type Date_Type is record Day : Day_Type; Month : Month_Type; Year : Year_Type; end record; for Date_Type use record Day at 0 range 0 .. 4; Month at 0 range 5 .. 8; Year at 0 range 9 .. 19; end record; for Date_Type'Size use 20; Year, Month, Day : Integer; Date : Date_Type; begin Get (Year); Date.Year := Year_Type (Year); Get (Month); Date.Month := Month_Type (Month); Get (Day); Date.Day := Day_Type (Day); end Packed_Date; The philosophy in Ada is: say what you want, and let the compiler do the dirty work for you. -- Ludovic Brenta.