Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.4 DATA STRUCTURES

5.4.8 Modular Types

guideline

  • Use modular types rather than Boolean arrays when you create data structures that need bit-wise operations, such as and and or.

  • example
    with Interfaces;
    procedure Main is
       type Unsigned_Byte is mod 255;
    
       X : Unsigned_Byte;
       Y : Unsigned_Byte;
       Z : Unsigned_Byte;
       X1 : Interfaces.Unsigned_16;
    begin -- Main
       Z := X or Y;  -- does not cause overflow
    show example of left shift
       X1 := 16#FFFF#;
       for Counter in 1 .. 16 loop
          X1 := Interfaces.Shift_Left (Value => X1, Amount => 1);
       end loop;
    end Main;
    

    rationale

    Modular types are preferred when the number of bits is known to be fewer than the number of bits in a word and/or performance is a serious concern. Boolean arrays are appropriate when the number of bits is not particularly known in advance and performance is not a serious issue. See also Guideline 10.6.3.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography