DEFINITION MODULE Bits;
(*******************************************************************
Module Bits (Version 1.0)
Copyright (c) 1989-2006 by Olivier Roth and ETH Zurich.
Purpose Manipulations of bits in bytes (8b) or words (16b).
Remarks Machine dependent!
Programming
o Design
Olivier Roth 18/02/1989
o Implementation
Olivier Roth 18/02/1989
ETH Zurich
Systems Ecology
CHN E 35.1
Universitaetstrasse 16
8092 Zurich
SWITZERLAND
URLs:
<mailto:RAMSES@env.ethz.ch>
<http://www.sysecol.ethz.ch>
<http://www.sysecol.ethz.ch/SimSoftware/RAMSES>
Last revision of definition: 08/03/1989 OR
*******************************************************************)
FROM SYSTEM IMPORT BYTE, WORD;
TYPE
typeID = ( byt, chr, dec, int, set, wrd );
WordTYPE = RECORD (* 16 bits *)
CASE : typeID OF
byt : b : ARRAY[0..1] OF BYTE;
| chr : c : ARRAY[0..1] OF CHAR;
| dec : d : CARDINAL;
| int : i : INTEGER;
| set : s : BITSET;
| wrd : w : WORD;
END(*CASE*);
END(*RECORD*);
ByteTYPE = RECORD (* 8 bits *)
CASE : typeID OF
byt : b : BYTE;
| chr : c : CHAR;
END(*CASE*);
END(*RECORD*);
BinSTR16 = ARRAY[0..15] OF CHAR;
OctSTR6 = ARRAY[0.. 5] OF CHAR;
HexSTR4 = ARRAY[0.. 3] OF CHAR;
BinSTR8 = ARRAY[0.. 7] OF CHAR;
OctSTR3 = ARRAY[0.. 2] OF CHAR;
HexSTR2 = ARRAY[0.. 1] OF CHAR;
VAR
Pow2 : ARRAY[0..15] OF CARDINAL;
HexDIGIT : ARRAY[0..15] OF CHAR;
(* ------------------------------------------------------------------- *)
(* Assignment and Conversion procedures for WordTYPE: *)
PROCEDURE WordToBinStr( a: WORD; VAR BinStr: BinSTR16 );
(* convert a two byte variable to an array of bits. *)
PROCEDURE BinStrToWord( BinStr: BinSTR16; VAR a: WORD );
(* convert an array of bits to a two byte variable. *)
PROCEDURE WordToOctStr( a: WORD; VAR OctStr: OctSTR6 );
(* convert a two byte variable to an array of OctDIGIT. *)
PROCEDURE OctStrToWord( OctStr: OctSTR6; VAR a: WORD );
(* convert an array of OctDIGIT to a two byte variable. *)
PROCEDURE WordToHexStr( a: WORD; VAR HexStr: HexSTR4 );
(* convert a two byte variable to an array of HexDIGIT. *)
PROCEDURE HexStrToWord( HexStr: HexSTR4; VAR a: WORD );
(* convert an array of HexDIGIT to a two byte variable. *)
(* ------------------------------------------------------------------- *)
(* BitManipulations for WordTYPE: *)
PROCEDURE ShiftLeftW( VAR a: WORD; n : CARDINAL );
(* shifts all bits of a n places to the left. *)
PROCEDURE CircShiftLeftW( VAR a: WORD; n : CARDINAL );
(* shifts all bits of a n places to the left, overflowing bits
are reentered at right. *)
PROCEDURE ShiftRightW( VAR a: WORD; n : CARDINAL );
(* shifts all bits of a n places to the right. *)
PROCEDURE CircShiftRightW( VAR a: WORD; n : CARDINAL );
(* shifts all bits of a n places to the right, overflowing bits
are reentered at left. *)
(* BitOperations for WordTYPE: *)
PROCEDURE NotW( a: WORD ) : WORD;
(* sets all bits of a to the opposite state. *)
PROCEDURE AndW( a, b : WORD ) : WORD;
(* equals the logical operation AND on the two variables a and b. *)
PROCEDURE OrW( a, b : WORD ) : WORD;
(* equals the logical operation OR on the two variables a and b. *)
PROCEDURE XorW( a, b : WORD ) : WORD;
(* equals the logical operation XOR on the two variables a and b. *)
(* ------------------------------------------------------------------- *)
(* Assignment and Conversion procedures for ByteTYPE: *)
PROCEDURE ByteToBinStr( a: BYTE; VAR BinStr: BinSTR8 );
(* convert a byte variable to an array of bits. *)
PROCEDURE BinStrToByte( BinStr: BinSTR8; VAR a: BYTE );
(* convert an array of bits to a byte variable. *)
PROCEDURE ByteToOctStr( a: BYTE; VAR OctStr: OctSTR3 );
(* convert a byte variable to an array of OctDIGIT. *)
PROCEDURE OctStrToByte( OctStr: OctSTR3; VAR a: BYTE );
(* convert an array of OctDIGIT to a byte variable. *)
PROCEDURE ByteToHexStr( a: BYTE; VAR HexStr: HexSTR2 );
(* convert a byte variable to an array of HexDIGIT. *)
PROCEDURE HexStrToByte( HexStr: HexSTR2; VAR a: BYTE );
(* convert an array of HexDIGIT to a byte variable. *)
(* ------------------------------------------------------------------- *)
(* BitManipulations for ByteTYPE: *)
PROCEDURE ShiftLeftB( VAR a: BYTE; n : CARDINAL );
(* shifts all bits of a n places to the left. *)
PROCEDURE CircShiftLeftB( VAR a: BYTE; n : CARDINAL );
(* shifts all bits of a n places to the left, overflowing bits
are reentered at right. *)
PROCEDURE ShiftRightB( VAR a: BYTE; n : CARDINAL );
(* shifts all bits of a n places to the right. *)
PROCEDURE CircShiftRightB( VAR a: BYTE; n : CARDINAL );
(* shifts all bits of a n places to the right, overflowing bits
are reentered at left. *)
(* BitOperations for ByteTYPE: *)
PROCEDURE NotB( a: BYTE ) : BYTE;
(* sets all bits of a to the opposite state. *)
PROCEDURE AndB( a, b : BYTE ) : BYTE;
(* equals the logical operation AND on the two variables a and b. *)
PROCEDURE OrB( a, b : BYTE ) : BYTE;
(* equals the logical operation OR on the two variables a and b. *)
PROCEDURE XorB( a, b : BYTE ) : BYTE;
(* equals the logical operation XOR on the two variables a and b. *)
END Bits.