comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Boolean Representation
Date: 1998/09/25
Date: 1998-09-25T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.98Sep25153102@spectre.mitre.org> (raw)
In-Reply-To: 6ue3uh$qjo$1@nnrp1.dejanews.com

In article <6ue3uh$qjo$1@nnrp1.dejanews.com> matthew_snyder@my-dejanews.com writes:

  > The reason for my original question is that I'm trying to decide
  > the best (most portable) way to convert an UNSIGNED_8 to a record
  > type which provides access to the individual bits. The record
  > could either consist of booleans, or it could consist of my own
  > bit type (is there a predefined BIT type that I'm overlooking?).
  > Right now my record consists of booleans and I'm performing an
  > unchecked conversion from UNSIGNED_8 to the record type. The
  > option of doing a "for RECORD'ADDRESS use SOME_ADDRESS" is not
  > available in this situation. Is any use of UNCHECKED_CONVERSION
  > considered "junky coding"?  I certainly would not want to have to
  > do a 'POS on every bit to assure portability.  What's the best way
  > to do this?

   Hmmm, do you really need the record type, or is access to the bits
enough?

 with Interfaces;
 package Bit_Manipulation is
 
   subtype Index is Integer range 1..8;

   function Get(X: Interfaces.Unsigned_8; Bit: Integer) return Boolean;
 
   procedure Set(X: in out Interfaces.Unsigned_8; Bit: in Integer);

   procedure Unset(X: in out Interfaces.Unsigned_8; Bit: in Integer);
   procedure Set(X: in out Interfaces.Unsigned_8;
                 Bit: in Integer;
                 Value: in Boolean);

   pragma Inline(Get, Set, Unset);

 end Bit_Manipulation;

 package body Bit_Manipulation is

   use Interfaces;
 
   Mask: constant array(Index) of Interfaces.Unsigned_8 := 
                                       (1,2,4,8,16,32,64,128);

   function Get(X: Interfaces.Unsigned_8; Bit: Integer) return Boolean is
   begin return (X and Mask(Bit)) /= 0; end Get;
        
   procedure Set(X: in out Interfaces.Unsigned_8; Bit: in Integer) is
   begin X := X or Mask(Bit); end Set;

   procedure Unset(X: in out Interfaces.Unsigned_8; Bit: in Integer) is
   begin X := X and not Mask(Bit); end Unset;

   procedure Set(X: in out Interfaces.Unsigned_8;
                 Bit: in Integer;
                 Value: in Boolean) is
   begin 
     if Value then Set(X, Bit); else Unset(X, Bit); end if;
   end Set;

 end Bit_Manipulation;

    I haven't checked compilers to see if the resulting code is what
you would expect for these operations, but it should come pretty close
as long as Bit is static.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  parent reply	other threads:[~1998-09-25  0:00 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-24  0:00 Boolean Representation matthew_snyder
1998-09-24  0:00 ` dewarr
1998-09-24  0:00   ` Samuel T. Harris
1998-09-25  0:00     ` dewarr
1998-09-27  0:00       ` Samuel T. Harris
1998-09-28  0:00         ` dewar
1998-09-24  0:00   ` matthew_snyder
1998-09-24  0:00     ` Tom Moran
1998-09-25  0:00       ` dewarr
1998-09-25  0:00         ` Tom Moran
1998-09-24  0:00     ` dennison
1998-09-25  0:00     ` Robert I. Eachus [this message]
1998-09-25  0:00     ` dewarr
1998-09-24  0:00 ` dennison
1998-09-24  0:00   ` Keith Thompson
1998-09-25  0:00     ` dennison
1998-09-25  0:00       ` Keith Thompson
1998-09-26  0:00         ` Tucker Taft
1998-09-26  0:00           ` Keith Thompson
1998-09-27  0:00             ` dewarr
1998-09-27  0:00             ` null pointer representation (was: Boolean Representation) Arthur Evans Jr
1998-09-27  0:00               ` Keith Thompson
1998-09-28  0:00               ` dewarr
1998-09-28  0:00                 ` Keith Thompson
1998-09-28  0:00                   ` dewarr
1998-09-30  0:00                     ` Keith Thompson
1998-10-02  0:00                       ` Robert I. Eachus
1998-09-28  0:00                 ` Lieven Marchand
1998-09-27  0:00           ` Boolean Representation dewarr
  -- strict thread matches above, loose matches on Subject: below --
1998-09-24  0:00 matthew_snyder
1998-09-25  0:00 ` Tucker Taft
1998-09-25  0:00   ` dewarr
1998-09-25  0:00   ` dewarr
replies disabled

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