From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Bit operations in Ada
Date: Fri, 23 May 2008 19:27:48 -0500
Date: 2008-05-23T19:27:48-05:00 [thread overview]
Message-ID: <g17nf4$s9a$1@jacob-sparre.dk> (raw)
In-Reply-To: wcc63t4ejd1.fsf@shell01.TheWorld.com
"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wcc63t4ejd1.fsf@shell01.TheWorld.com...
> Dennis Hoppe <dennis.hoppe@hoppinet.de> 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.
next prev parent reply other threads:[~2008-05-24 0:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-23 21:19 Bit operations in Ada Dennis Hoppe
2008-05-23 22:08 ` Ludovic Brenta
2008-05-24 15:36 ` Simon Wright
2008-06-02 13:27 ` Bit operations in Ada (endianness) Dennis Hoppe
2008-06-02 14:01 ` (see below)
2008-06-02 18:22 ` Jeffrey R. Carter
2008-06-02 17:38 ` Ludovic Brenta
2008-05-23 22:38 ` Bit operations in Ada Robert A Duff
2008-05-24 0:27 ` Randy Brukardt [this message]
2008-05-24 9:40 ` Bit operations in Ada (Thank you) Dennis Hoppe
2008-05-23 23:25 ` Bit operations in Ada Jeffrey R. Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox