DDC-I Logo

Sitemap  

DDC-I Ada Compiler System  

Isolating Single Bits

Product Family: DACS, SCORE, TADS
Target CPU: Any
Language: Ada
Host: Any

Isolating single bits can be achieved in several manners. This note describes a method which is particularly efficient with DACS-80x86.

The mod operator is the most efficient way to isolate a single bit, if the value after the mod is a power of 2:

bit0 := my_value mod 2; 
       -- retrieves least significant bit, despite sign

To retrieve bit 2, combine with division:

bit2 := my_value mod 2**4 / 2**3;
       -- retrieves 4th bit from the right

or more general (insert literal value for n):

bitn := my_value mod (2**n) / (2**(n-1));
       -- retrieves nth bit from the right

The n value must be maximum number of bits total - 2 (for 16 bit: 14, for 32 bit 30) as otherwise the constants used overflow. To get to all bits of a M-bit word the use of unchecked_conversion can be recommended:

Procedure get_bits Is
    type bitsM_array is array( 0 .. M-1 ) of boolean;
    pragma pack( bits_array );

    function mk_bits is new_unchecked_conversion( intM_type, bitsM_array );
begin         -- procedure get_bits
    bitn := mk_bits(valM)(n);
end get_bits;

These methods will work with all Ada compilers, though they may not be the most efficient for a given compiler.

Contact
602-275-7172
sales@ddci.com

IDIQ Contract Vehicles:
--------------
AMCOM Express
DESP II
F2AST
R23G

Links

Support

Members Area
    -Member Login/Return
    -Login Help

Atlas Support Packages
    -Atlas Premium
    -Atlas Advantage
    -Atlas Choice

Complimentary Support

Submit a Software Trouble Report

Customer Quote:
"DDC-I has a good solution for us and your willingness to get down to the issues and help us get what we need has made it a clear choice."