home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / bitfield.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.8 KB  |  70 lines

  1.                                    -- Chapter 30 - Program 2
  2. with Text_IO, Unchecked_Conversion;
  3. use Text_IO;
  4.  
  5. procedure BitField is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    type BITS is record
  11.       Lower  : INTEGER range 0..3;
  12.       Middle : INTEGER range 0..1;
  13.       High   : INTEGER range 0..3;
  14.    end record;
  15.  
  16.    for BITS use record
  17.       Lower  at 0 range 0..1;
  18.       Middle at 0 range 2..2;
  19.       High   at 0 range 3..4;
  20.    end record;
  21.  
  22.    BitData : BITS;
  23.    IntData : INTEGER;
  24.  
  25.    function Switch_To_Bits is new Unchecked_Conversion(
  26.                                    Source => INTEGER,
  27.                                    Target => BITS);
  28.  
  29. begin
  30.  
  31.    BitData.Lower := 2;
  32.    BitData.High := 3;
  33.    BitData.Middle := 0;
  34.  
  35.    for Index in 1..18 loop
  36.       IntData := Index + 5;
  37.       BitData := Switch_To_Bits(IntData);
  38.       Put("IntData =");
  39.       Put(IntData,4);
  40.       Put("   BitData =");
  41.       Put(INTEGER(BitData.High),3);
  42.       Put(INTEGER(BitData.Middle),3);
  43.       Put(INTEGER(BitData.Lower),3);
  44.       New_Line;
  45.    end loop;
  46.  
  47. end BitField;
  48.  
  49. -- Result of Execution
  50.  
  51. -- IntData =   6   BitData =  0  1  2
  52. -- IntData =   7   BitData =  0  1  3
  53. -- IntData =   8   BitData =  1  0  0
  54. -- IntData =   9   BitData =  1  0  1
  55. -- IntData =  10   BitData =  1  0  2
  56. -- IntData =  11   BitData =  1  0  3
  57. -- IntData =  12   BitData =  1  1  0
  58. -- IntData =  13   BitData =  1  1  1
  59. -- IntData =  14   BitData =  1  1  2
  60. -- IntData =  15   BitData =  1  1  3
  61. -- IntData =  16   BitData =  2  0  0
  62. -- IntData =  17   BitData =  2  0  1
  63. -- IntData =  18   BitData =  2  0  2
  64. -- IntData =  19   BitData =  2  0  3
  65. -- IntData =  20   BitData =  2  1  0
  66. -- IntData =  21   BitData =  2  1  1
  67. -- IntData =  22   BitData =  2  1  2
  68. -- IntData =  23   BitData =  2  1  3
  69.  
  70.