[comp.lang.ada] Question with setting sign bit

west@widgit.enet.dec.com (Jim West (Stealth Contractor)) (12/06/90)

In article <18172@hydra.gatech.EDU>, 
ms33@prism.gatech.EDU (Mike Sullivan,  a.k.a. Sully) writes...

> 
>  I have a problem setting the sign-bit in an integer while trying to
>  explicitly set particular bits (as opposed to storing a negative
>  value.)  The problem seems to caused by the strong typing of Ada in
>  conjunction with a limitation of the compilor.  Ada wants me to use
>  unsigned integers.  The compilor I use does not have unsigned integers
>  because the processor does not support unsigned arithmetic.  I have
>  words that are coming off of a 1553 bus, and a general-purpose package
>  passing the information on to me.  I want to see if some bits are set,
>  or not set as the case may be.  There is different situation where I
> 

  Have you considered using a bit array ?  I've done this many times.  I
find it far easier to do bit twiddling this way.  An example would look
something like :

    array_size	: constant := 15

    type bit is range 0 .. 1;
    for bit'size use 1;

    type data_type is array (0 .. array_size) of bit;
    pragma pack (data_type);

    data	: data_type;

  You could then use this in conjunction with a for-use-at clause and lay
the array on top of a record, or any other data type for manipulation.

					-=> Jim <=-

----------------------------------------------------------------------
 Jim West                      |  The Schainker Converse
 west@widgit.enet.dec.com      |  to Hoare's Law :
                               |
 These are my opinions.        |   Inside every small problem
 Digital has no idea           |     is a larger problem struggling
 what I'm  saying.             |       to get out.
----------------------------------------------------------------------

beser@tron.UUCP (Eric Beser) (12/13/90)

The sign bit on a system that does not support unsigned arithmetic
can be a "sticky" situation. However, where you are just checking
a single bit, the solution is pretty easy.

declare

    type bit_array_32 is array (0 .. 31) of boolean;
    pragma pack(bit_array_32);
   
    function to_bit_array_32 is new unchecked_conversion 
					(source => integer,
					 target => bit_array_32);

    function to_integer is new unchecked_conversion
					(source => bit_array_32,
					 target => integer);

    check_word   : bit_array_32;
begin
    check_word := to_bit_array_32(function_reading_integer);
    if check_word(31)  then  -- sign bit set
        -- do what you have to

    end if;

end

this may be turned into an inline function that returns boolean, in
which case it would look like

function is_sign_bit_set (on_the_integer : integer) return boolean is

    same declarations as above with the exception

    check_word : bit_array_32 := to_bit_array_32(on_the_integer);
begin
    return check_word(31);

end is sign_bit_set;

pragma inline (is_sign_bit_set);


if speed is a requirement, and your compiler supports it, use machine
code inserted procedures (within the ada function) and pragma inline
that. On the XD 1750a compiler, we can do that in about 3 lines of code.

Hope this helps

Eric Beser
Senior Ada Technologist
Westinghouse Electronic Systems Group
(301) - 765 - 1010

ebeser@ajpo.sei.cmu.edu