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,705f377a5e04a446 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!nuzba.szn.dk!news.jacob-sparre.dk!pnx.dk!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Bit operations in Ada Date: Fri, 23 May 2008 19:27:48 -0500 Organization: Jacob's private Usenet server Message-ID: References: NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: jacob-sparre.dk 1211588900 28970 69.95.181.76 (24 May 2008 00:28:20 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Sat, 24 May 2008 00:28:20 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5512 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512 Xref: g2news1.google.com comp.lang.ada:312 Date: 2008-05-23T19:27:48-05:00 List-Id: "Robert A Duff" wrote in message news:wcc63t4ejd1.fsf@shell01.TheWorld.com... > Dennis Hoppe writes: > >> I'm new to Ada and bitwise operations is a new challenge in this >> realm. My objective is to manipulate some bit strings in Ada, especially: >> >> a) addition/subtraction mod 2**n, >> b) change bits directly (e.g, via array access) >> c) shift operations >> d) rotate operations >> e) and, xor, not, or > > I'm curious why you want to do all these things on the same type. > You say, "My objective is...", but what's the higher-level objective? I agree with Bob. Most of the time, you are better off not doing bit operations explicitly at all in Ada: let the compiler do them instead. That is to say, use record types and representation clauses to specify the structures you need, and let the compiler do all of the work. For instance, I have a card game evaluation program that starts like this: type Suits is (Hearts, Diamonds, Spades, Clubs); subtype Red_Suits is Suits range Hearts .. Diamonds; subtype Black_Suits is Suits range Spades .. Clubs; type Pips is (Empty, Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King); type Card_Type is record Pip : Pips; Suit : Suits; end record; for Card_Type use record Pip at 0 range 0 .. 3; Suit at 0 range 4 .. 5; end record; for Card_Type'Size use 6; NO_CARD : constant Card_Type := (Suit => Hearts, Pip => Empty); type Card_Array_Type is array (Natural range <>) of Card_Type; for Card_Array_Type'Component_Size use 6; This lets code find out the suit or pip value of a card separately, while still providing a convinient way to do operations on a whole card or list of cards. And with little waste of space. (I should point out that the application actually ended up using a different way to compress arrays of cards, using knowledge of the starting position to save lots more space, and ultimately, Card_Array_Type was changed to use 8 bit elements for better performance. Beware of premature optimization.) There are a few cases where you really need to use shifts explicitly (encryption comes to mind), but then the need for single bit access is non-existent. In those cases, the modular types in Interfaces are your best bet. Randy.