comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Newbie question: How does one do bit manipulation in Ada?
Date: Sun, 28 Dec 2003 13:40:12 -0500
Date: 2003-12-28T13:40:12-05:00	[thread overview]
Message-ID: <bN6dnYXsR6wQvnKiRVn-ug@comcast.com> (raw)
In-Reply-To: <MPG.1a58a29890dcd92f989686@news.sover.net>

Peter C. Chapin wrote:

> Anyway, I have yet to deal with writing CRC checksumming code in Ada 
> (that's for later). At the moment I'm getting my noisy channel 
> simulation to work. My Transceive procedure works by looping over the 
> eight bits in an octet and, if a particular bit is to be corrupted, 
> XORing a suitable mask into the number to invert the bit.

You could certainly do this in Ada with say:

type Octet is mod 256;
for Octet'Size use 8; -- optional, doesn't affect semantics.

But first you should think about where you want to go with this.  Right 
now you are doing unbiased errors.  You could also OR 1 and AND 0 error 
bits in to create a bias toward one or zero.  You might also want to 
implement the error generator as a finite state machine to create burst 
errors:

   type State is (Clear, Burst);
   Current: State := Clear;
   function Error return Boolean is
   begin
     case Current is
       when Clear =>
         if Random < 0.01
         then
           Current := Burst;
           return True;
         else
           Return False;
         end if;
       when Burst =>
         if Random < 0.5
         then
           Current := Clear;
           return False;
         else
           return True;
         end if;
     end case;
   end Error;

(You really should start the error generator in a state chosen based on 
the calculated probability from the generator.  I'd just throw the first 
few dozen bytes away...)

As for the choice between a boolean array and a modular type for Octet, 
it is a close call. Or you could even convert back and forth. 
Personally, I would use the modular type, and generate error bytes by:

function Error_Octet is
   Result: Octet := 0;
begin
   for I in 1..8 loop
     Result := Result + Result; -- or 2 * Result;
     if Error then Result := Result + 1;
   end loop;
   return Result;
end Error_Octet;

Seems simple enough, and allows Error to be as complex a function as you 
want.



In any case, I think you probably want to count an octet as an error if 
any bits were flipped, and are not really planning on counting the 
number of errors in a


> 
> The C program that I wrote originally uses the type int for the 
> simulated octets. My first pass at an Ada translation thus used Integer. 
> However, I can see that Integer is not entirely the most appropriate 
> type here so I have no problem using one of the modular types (or a 
> subtype thereof). In fact, I could even use a record type of some kind 
> if there was some advantage to doing so. I don't actually use the data 
> for anything aside from this simulation; an array of 8 boolean values 
> would work for me.
> 
> Hmmmm.
> 
> Peter

-- 
                                           Robert I. Eachus

"The war on terror is a different kind of war, waged capture by capture, 
cell by cell, and victory by victory. Our security is assured by our 
perseverance and by our sure belief in the success of liberty." -- 
George W. Bush




  reply	other threads:[~2003-12-28 18:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23  1:58 Newbie question: How does one do bit manipulation in Ada? Peter C. Chapin
2003-12-23  3:01 ` Georg Bauhaus
2003-12-23  3:23   ` Peter C. Chapin
2003-12-23  3:36     ` tmoran
2003-12-23  3:33 ` Steve
2003-12-23  9:09 ` Stephen Leake
2003-12-28 13:20   ` Peter C. Chapin
2003-12-28 18:40     ` Robert I. Eachus [this message]
2003-12-29  2:57       ` Peter C. Chapin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox