comp.lang.ada
 help / color / mirror / Atom feed
* bit operations on integers
@ 1999-05-03  0:00 phadreus
  1999-05-03  0:00 ` David C. Hoos, Sr.
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: phadreus @ 1999-05-03  0:00 UTC (permalink / raw)




How do I test, set, and clear individual bits in an integer
in Ada 83?

Thank you.


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 bit operations on integers phadreus
  1999-05-03  0:00 ` David C. Hoos, Sr.
  1999-05-03  0:00 ` Matthew Heaney
@ 1999-05-03  0:00 ` Jerry Petrey
  1999-05-04  0:00 ` Vincent P. Amiot
  3 siblings, 0 replies; 7+ messages in thread
From: Jerry Petrey @ 1999-05-03  0:00 UTC (permalink / raw)


phadreus@iname.com wrote:
> 
> How do I test, set, and clear individual bits in an integer
> in Ada 83?
> 
> Thank you.
> 
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


Many Ada 83 compilers (like VADS from Rational) have a bit operations
package for doing these operations.  In VADS it was called Bit_Ops .

Jerry

-- 
=====================================================================
=  Jerry Petrey - Consultant Software Engineer  - Member Team Ada   =
=                 Lockheed Martin                 Member Team Forth =
=====================================================================




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 bit operations on integers phadreus
  1999-05-03  0:00 ` David C. Hoos, Sr.
@ 1999-05-03  0:00 ` Matthew Heaney
  1999-05-03  0:00   ` dennison
  1999-05-03  0:00 ` Jerry Petrey
  1999-05-04  0:00 ` Vincent P. Amiot
  3 siblings, 1 reply; 7+ messages in thread
From: Matthew Heaney @ 1999-05-03  0:00 UTC (permalink / raw)


phadreus@iname.com writes:

> How do I test, set, and clear individual bits in an integer
> in Ada 83?

Convert the object to an array of Booleans:

 type Integer_8 is range 0 .. 255;

 for Integer_8'Size use 8;


 type Boolean_Array is (Natural range <>) of Boolean;
 pragma Pack (Boolean_Array);

 subtype Boolean_Array_8 is Boolean_Array (0 .. 7);

 function To_Boolean_Array is 
   new Unchecked_Conversion (Integer_8, Boolean_Array_8);

 function To_Integer_8 is
   new Unchecked_Conversion (Boolean_Array_8, Integer_8);


 procedure Set_Bit (I : in out Integer_8; N : in Natural) is

    BA : Boolean_Array_8 := To_Boolean_Array (I);

 begin

    BA (N) := True;   -- BA (7 - N) on a big endian machine

    I := To_Integer_8 (BA);

  end Set_Bit;


  procedure Clear_Bit ...

  function Get_Bit ...


 declare
   I : Integer_8;
 begin

   Set_Bit (I, 2);


   
 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 ` Matthew Heaney
@ 1999-05-03  0:00   ` dennison
  0 siblings, 0 replies; 7+ messages in thread
From: dennison @ 1999-05-03  0:00 UTC (permalink / raw)


In article <m34slut6ur.fsf@mheaney.ni.net>,
  Matthew Heaney <matthew_heaney@acm.org> wrote:
>  procedure Set_Bit (I : in out Integer_8; N : in Natural) is

>   procedure Clear_Bit ...
>
>   function Get_Bit ...

I think these operations are a "bit" of overkill. (cue rimshot)

But YMMV.

--
T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 bit operations on integers phadreus
@ 1999-05-03  0:00 ` David C. Hoos, Sr.
  1999-05-03  0:00   ` Keith Thompson
  1999-05-03  0:00 ` Matthew Heaney
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: David C. Hoos, Sr. @ 1999-05-03  0:00 UTC (permalink / raw)



phadreus@iname.com wrote in message <7gkhr7$5kr$1@nnrp1.dejanews.com>...
>
>
>How do I test, set, and clear individual bits in an integer
>in Ada 83?
>
How about:

package Bit_Ops is

   function Test
     (Item : Integer;
      N    : Natural)
      return Boolean;

   procedure Set
     (Item : in out Integer;
      N    :        Natural);

   procedure Clear
     (Item : in out Integer;
      N    :        Natural);

end Bit_Ops;

package body Bit_Ops is

   type Bit_Array is
      array (Natural range 0 .. Integer'Size - 1)
     of Boolean;
   pragma Pack (Bit_Array);


   -----------
   -- Clear --
   -----------

   procedure Clear
     (Item : in out Integer;
      N    :        Natural)
   is
     Bits : Bit_Array;
     for Bits use at Item'Address;
   begin
      Bits (N) := False;
   end Clear;

   ---------
   -- Set --
   ---------

   procedure Set
     (Item : in out Integer;
      N    :        Natural)
   is
     Bits : Bit_Array;
     for Bits use at Item'Address;
   begin
      Bits (N) := True;
   end Set;

   ----------
   -- Test --
   ----------

   function Test
     (Item : Integer;
      N    : Natural)
      return Boolean
   is
     Bits : Bit_Array;
     for Bits use at Item'Address;
   begin
      return Bits (N);
   end Test;

end Bit_Ops;









^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 ` David C. Hoos, Sr.
@ 1999-05-03  0:00   ` Keith Thompson
  0 siblings, 0 replies; 7+ messages in thread
From: Keith Thompson @ 1999-05-03  0:00 UTC (permalink / raw)


"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:
[...]
>    procedure Clear
>      (Item : in out Integer;
>       N    :        Natural)
>    is
>      Bits : Bit_Array;
>      for Bits use at Item'Address;
>    begin
>       Bits (N) := False;
>    end Clear;
[...]

In Ada 83, using an address clause to achieve an overlay is erroneous.
(In Ada 95, it's erroneous only if it violates the properties of the
type, or words to that effect.)  At least one Ada 83 compiler issues a
warning for such a construct.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center  <http://www.sdsc.edu>                 <*>
Techno-geek.  Mouse bigger than phone.  Bites heads off virtual chickens.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: bit operations on integers
  1999-05-03  0:00 bit operations on integers phadreus
                   ` (2 preceding siblings ...)
  1999-05-03  0:00 ` Jerry Petrey
@ 1999-05-04  0:00 ` Vincent P. Amiot
  3 siblings, 0 replies; 7+ messages in thread
From: Vincent P. Amiot @ 1999-05-04  0:00 UTC (permalink / raw)


On Aonix/Alsys compilers there is a package 'Unsigned' that defines "and"
and "or" for 8 and 16 bit integers:

	-- test Bit4
	if (X and 2#0001_0000#) > 0 then

you can always resort to unchecked-convert to packed arrays of booleans of
the right size and implement your own.

A side note:
To represent 32 bit int in Ada83 when BIT31 is set (most of the Ada83
compiler do no accept: type X is range 0 .. 2**32-1; -- this would
theoretically require the availability of a 33 bit integer base type...

	Bias : constant := 2**32;

	K : constant :=  16#AAAA_BBBB#  - BIAS;

same bit pattern but negative!

Have fun!

Vincent Amiot
Principal Consultant / Aonix
-- 
 


phadreus@iname.com wrote in article <7gkhr7$5kr$1@nnrp1.dejanews.com>...
> 
> 
> How do I test, set, and clear individual bits in an integer
> in Ada 83?
> 
> Thank you.
> 
> 
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own  
 
> 




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-05-04  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-05-03  0:00 bit operations on integers phadreus
1999-05-03  0:00 ` David C. Hoos, Sr.
1999-05-03  0:00   ` Keith Thompson
1999-05-03  0:00 ` Matthew Heaney
1999-05-03  0:00   ` dennison
1999-05-03  0:00 ` Jerry Petrey
1999-05-04  0:00 ` Vincent P. Amiot

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