home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 30 - Program 2
- with Text_IO, Unchecked_Conversion;
- use Text_IO;
-
- procedure BitField is
-
- package Int_IO is new Text_IO.Integer_IO(INTEGER);
- use Int_IO;
-
- type BITS is record
- Lower : INTEGER range 0..3;
- Middle : INTEGER range 0..1;
- High : INTEGER range 0..3;
- end record;
-
- for BITS use record
- Lower at 0 range 0..1;
- Middle at 0 range 2..2;
- High at 0 range 3..4;
- end record;
-
- BitData : BITS;
- IntData : INTEGER;
-
- function Switch_To_Bits is new Unchecked_Conversion(
- Source => INTEGER,
- Target => BITS);
-
- begin
-
- BitData.Lower := 2;
- BitData.High := 3;
- BitData.Middle := 0;
-
- for Index in 1..18 loop
- IntData := Index + 5;
- BitData := Switch_To_Bits(IntData);
- Put("IntData =");
- Put(IntData,4);
- Put(" BitData =");
- Put(INTEGER(BitData.High),3);
- Put(INTEGER(BitData.Middle),3);
- Put(INTEGER(BitData.Lower),3);
- New_Line;
- end loop;
-
- end BitField;
-
- -- Result of Execution
-
- -- IntData = 6 BitData = 0 1 2
- -- IntData = 7 BitData = 0 1 3
- -- IntData = 8 BitData = 1 0 0
- -- IntData = 9 BitData = 1 0 1
- -- IntData = 10 BitData = 1 0 2
- -- IntData = 11 BitData = 1 0 3
- -- IntData = 12 BitData = 1 1 0
- -- IntData = 13 BitData = 1 1 1
- -- IntData = 14 BitData = 1 1 2
- -- IntData = 15 BitData = 1 1 3
- -- IntData = 16 BitData = 2 0 0
- -- IntData = 17 BitData = 2 0 1
- -- IntData = 18 BitData = 2 0 2
- -- IntData = 19 BitData = 2 0 3
- -- IntData = 20 BitData = 2 1 0
- -- IntData = 21 BitData = 2 1 1
- -- IntData = 22 BitData = 2 1 2
- -- IntData = 23 BitData = 2 1 3
-
-