TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Steven Deller <[log in to unmask]>
Reply To:
Steven Deller <[log in to unmask]>
Date:
Tue, 16 Apr 2002 00:12:23 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (111 lines)
Why aren't you using modular types and simple masks?

   type register is mod 2**8 ;
   for register'size use 8 ;
   x,y : register ;
   ...
   x := y and 2#1010_1010# ;
   y := x or 2#0101_0101# ;
   x := x xor y ;
   ...
and you can do things like the following in an iteration:

   subtype bit_number is natural range 0..7 ;
   for I in bit_number'range loop
       -- Turn off odd bits in x using a mask with
       -- all bits on except the bit in question if I is odd
       x := x and ( not Register( (I mod 2) * 2**I ) ) ;
   end loop ;

or, perhaps more clearly
   for I in bit_number'range loop
        if I mod 2 then
            x := x and (not register(2**I)) ;
        end if ;
   end loop ;

and so on.

If you insist on using "On" and "Off" then make a boolean type, use a
boolean *packed array* (not a record type) and get the same ability,
except the constants are a bit more awkward:
   x := y and (On,Off,On,Off,On,Off,On,Off) ;
On the positive side, using an array does allow for more explicit
iteration across bits.

Regards,
Steve

> ----- Original Message -----
> From: "Carlos Luque Dengra" <[log in to unmask]>
> To: <[log in to unmask]>
> Sent: Monday, April 15, 2002 1:11 AM
> Subject: Some questions on record attributes
>
>
> Hi,
> for the development for the port parallel interface, I've
> planned to map the unsigned_8 value to a record of bits of
> type (on,off). Do you know how to mask an integer or unsigned_8?
>
> As far as I haven't found an ellgant way I've done this:
>
> I've the next definitions:
>
> type flag is (on, off);
>
>  type Registro_Byte is
>       record
>          Bit0,
>          Bit1,
>          Bit2,
>          Bit3,
>          Bit4,
>          Bit5,
>          Bit6,
>          Bit7 : Flag;
>       end record;
>
> data : registro_byte;
> register : unsigned_8;
>
>
> (...) I load an 8-bit integer into the register:
>
> register:= 128; (this is done via DOSPORTS libraries)
>
> I want data to be a map of the register bit:
>
>     if Integer (Shift_Right (register , 0) mod 2 ) = 1 then
>          data.Bit0 := On;
>       else
>          data.Bit0 := Off;
>       end if;
>
>
>     if Integer (Shift_Right (register , 1) mod 2 ) = 1 then
>          data.Bit1 := On;
>       else
>          data.Bit1 := Off;
>       end if;
>
> and so on 'til BIT7.
>
> Do you know how to assign the values using a loop?
>
> I don't know how to do a loop that works like the next false one:
>
> for ix in 0..7 loop
>   if Integer (Shift_Right (register , ix ) mod 2 ) = 1 then
>          data.BIT(ix) := On; -- wrong line
>       else
>          data.Bit(ix) := Off; -- wrong line
>       end if;
> end loop;
>
>
> Thank you in advance.
>
> Carlos
>

ATOM RSS1 RSS2