* Newbie question: How does one do bit manipulation in Ada?
@ 2003-12-23 1:58 Peter C. Chapin
2003-12-23 3:01 ` Georg Bauhaus
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Peter C. Chapin @ 2003-12-23 1:58 UTC (permalink / raw)
Hello!
I'm a C/C++ programmer in the process of teaching myself Ada. I've been
writing toy programs to get a feeling for the language. There is
obviously a lot to learn, but I'm enjoying myself.
Anyway, I have a need to do some bit manipulations in one of my
programs. In particular I want to invert certain bits in a number
(integer). I'm used to doing this with a bitwise XOR operator (C/C++) so
I looked around for some kind of equivalent in Ada but I didn't find
anything in package Standard or in any of the "usual" library packages.
Am I missing something or am I just thinking about this wrong?
Thanks!
Peter
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
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:33 ` Steve
2003-12-23 9:09 ` Stephen Leake
2 siblings, 1 reply; 9+ messages in thread
From: Georg Bauhaus @ 2003-12-23 3:01 UTC (permalink / raw)
Peter C. Chapin <pchapin@sover.net> wrote:
: Anyway, I have a need to do some bit manipulations in one of my
: programs. In particular I want to invert certain bits in a number
: (integer). I'm used to doing this with a bitwise XOR operator (C/C++) so
: I looked around for some kind of equivalent in Ada but I didn't find
: anything in package Standard or in any of the "usual" library packages.
: Am I missing something or am I just thinking about this wrong?
xor is available for Boolean types, modular types, and one-dimensional
arrays of Booleans, as well as for (Wide_)Character_Sets.
Some signed and modular integer types are predefined in package
Interfaces.
If you cannot use the modular variant of integers, but you know that
some integers values in question are bitwise represented like modular
values, you could do an unchecked_conversion.
In addition, package Interfaces has Shift_Left/Right,
Shift_Right_Arithmentic, and Rotate_Left/Right.
It is also possible to use representation clauses and
assign names to bits or groups of bits that are components
of a record (and possibly again do unchecked_conversions
if needed).
-- Georg
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
2003-12-23 3:01 ` Georg Bauhaus
@ 2003-12-23 3:23 ` Peter C. Chapin
2003-12-23 3:36 ` tmoran
0 siblings, 1 reply; 9+ messages in thread
From: Peter C. Chapin @ 2003-12-23 3:23 UTC (permalink / raw)
In article <bs8b5o$3h1$1@a1-hrz.uni-duisburg.de>, sb463ba@l1-hrz.uni-
duisburg.de says...
> xor is available for Boolean types, modular types, and one-dimensional
> arrays of Booleans, as well as for (Wide_)Character_Sets.
> Some signed and modular integer types are predefined in package
> Interfaces.
Okay, thanks. I didn't look at package Interfaces, but obviously I
should. I think that will get me going.
Peter
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
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:33 ` Steve
2003-12-23 9:09 ` Stephen Leake
2 siblings, 0 replies; 9+ messages in thread
From: Steve @ 2003-12-23 3:33 UTC (permalink / raw)
Ada only permits bit masking on arrays of booleans or modular types. To do
an XOR on integers convert the integers to modular types using unchecked
conversion, xor the resulting modular values, and convert the result back to
integer. This question came up recently, here's the sample program I posted
earlier.
with Ada.Integer_Text_IO;
with Ada.Text_IO;
with Ada.Unchecked_Conversion;
procedure DoIntXOR is
function "xor"(Left,Right : in Integer) return Integer is
pragma Inline("xor");
type Int_As_Mod is mod 2 ** Integer'Size;
function To_Mod is
new Ada.Unchecked_Conversion( Integer, Int_As_Mod );
function To_Int is
new Ada.Unchecked_Conversion( Int_As_Mod, Integer );
begin
return To_Int( To_Mod(Left) xor To_Mod(Right) );
end "xor";
a_value : Integer;
b_value : Integer;
begin
Ada.Text_IO.Put( "Enter first value > " );
Ada.Integer_Text_IO.Get( a_value );
Ada.Text_IO.Put( "Enter second value > " );
Ada.Integer_Text_IO.Get( b_value );
Ada.Text_IO.Put( "XOR value is " );
Ada.Integer_Text_IO.Put( a_value xor b_value );
Ada.Text_IO.New_Line;
end DoIntXOR;
BTW: Unless you're doing interface work, you rarely need to do bit fiddling
in Ada.
Steve
(The Duck)
"Peter C. Chapin" <pchapin@sover.net> wrote in message
news:MPG.1a516ba5b76b5bee989684@news.sover.net...
>
> Hello!
>
> I'm a C/C++ programmer in the process of teaching myself Ada. I've been
> writing toy programs to get a feeling for the language. There is
> obviously a lot to learn, but I'm enjoying myself.
>
> Anyway, I have a need to do some bit manipulations in one of my
> programs. In particular I want to invert certain bits in a number
> (integer). I'm used to doing this with a bitwise XOR operator (C/C++) so
> I looked around for some kind of equivalent in Ada but I didn't find
> anything in package Standard or in any of the "usual" library packages.
> Am I missing something or am I just thinking about this wrong?
>
> Thanks!
>
> Peter
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
2003-12-23 3:23 ` Peter C. Chapin
@ 2003-12-23 3:36 ` tmoran
0 siblings, 0 replies; 9+ messages in thread
From: tmoran @ 2003-12-23 3:36 UTC (permalink / raw)
>Okay, thanks. I didn't look at package Interfaces, but obviously I
You can define your own modular types, or arrays of booleans, or packed
bit fields in a representation clause. Interfaces does conveniently
predefine the sort of thing you are used to in C, though.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
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:33 ` Steve
@ 2003-12-23 9:09 ` Stephen Leake
2003-12-28 13:20 ` Peter C. Chapin
2 siblings, 1 reply; 9+ messages in thread
From: Stephen Leake @ 2003-12-23 9:09 UTC (permalink / raw)
To: comp.lang.ada
Peter C.Chapin <pchapin@sover.net> writes:
> I'm a C/C++ programmer in the process of teaching myself Ada. I've been
> writing toy programs to get a feeling for the language. There is
> obviously a lot to learn, but I'm enjoying myself.
Good to hear!
> Anyway, I have a need to do some bit manipulations in one of my
> programs. In particular I want to invert certain bits in a number
> (integer).
Others have told you how to do this using XOR.
I'd like to point out that Ada often has better ways to accomplish
things that are done with bit-fiddling in other languages. So if you
can explain why you need to invert those bits, we might be able to
give you a better way to accomplish the same thing.
For example, specifying the exact position of bits in registers is
much simpler in Ada than in C.
--
-- Stephe
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
2003-12-23 9:09 ` Stephen Leake
@ 2003-12-28 13:20 ` Peter C. Chapin
2003-12-28 18:40 ` Robert I. Eachus
0 siblings, 1 reply; 9+ messages in thread
From: Peter C. Chapin @ 2003-12-28 13:20 UTC (permalink / raw)
In article <mailman.170.1072170574.31149.comp.lang.ada@ada-france.org>,
stephen_leake@acm.org says...
> Others have told you how to do this using XOR.
>
> I'd like to point out that Ada often has better ways to accomplish
> things that are done with bit-fiddling in other languages. So if you
> can explain why you need to invert those bits, we might be able to
> give you a better way to accomplish the same thing.
Sorry about the long delay for this follow-up; I've been away for the
Christmas holiday.
Anyway, I can appreciate that there might be a better way to accomplish
what I'm trying to do. My little program is trying to experiment with
the effectiveness of different checksumming techniques. I made a package
that simulates a noisy channel (random noise). The package has a
procedure to set the bit error rate and another procedure, Transceive,
that is intended to accept an 8 bit octet and then randomly change some
bits in that octet according to the selected bit error rate. It then
hands back the result.
The main program generates random data, computes checksums on this data
in various ways, and then passes the data, together with the checksums,
through the simulated noisy channel. By comparing the result with the
original data it can count the number of erronous bits. It can also
verify the checksum and thus find out if the checksum used was able to
detect the error. After using a large number of data blocks one can
accumulate statistically significant results. The program shows, for
example, that your typical CRC checksum does a much better job at
detecting errors than, for example, a simple checksum using the same
space overhead. There is nothing revolutionary in this, of course. As I
said, this is just a toy program that I'm using right now to help me
learn Ada.
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.
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
2003-12-28 13:20 ` Peter C. Chapin
@ 2003-12-28 18:40 ` Robert I. Eachus
2003-12-29 2:57 ` Peter C. Chapin
0 siblings, 1 reply; 9+ messages in thread
From: Robert I. Eachus @ 2003-12-28 18:40 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question: How does one do bit manipulation in Ada?
2003-12-28 18:40 ` Robert I. Eachus
@ 2003-12-29 2:57 ` Peter C. Chapin
0 siblings, 0 replies; 9+ messages in thread
From: Peter C. Chapin @ 2003-12-29 2:57 UTC (permalink / raw)
In article <bN6dnYXsR6wQvnKiRVn-ug@comcast.com>, rieachus@comcast.net
says...
> Seems simple enough, and allows Error to be as complex a function as you
> want.
Thanks for your comments. I will probably experiment with a few of your
ideas. I like the thought of making Error a separate function, for
example. I hadn't planned on playing around with burst errors, but I
might do so. At least it makes sense to lay the groundwork for doing so
in a future version.
Peter
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2003-12-29 2:57 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2003-12-29 2:57 ` Peter C. Chapin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox